Leaderstats, and purchasing methods

Created by stormmaster9090 -- advanced scripting--

by stormmaster9090

Author Avatar

Firstly we need to create the leaderstats. For this we need to use a script in ServerScriptService.

To do this you will need to make an IntValue in the script. Creating a function when a player is added.

looking at this block of code we can decipher a few things, first this is a script so it runs on the server side, the reason we have function(player) is so that we can determine who the local player is.

NumberValues and IntValues are different but still sort of alike, they both can hold a number but a int value has to hold an integer.

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	-----------
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	Cash.Value = 140
end)

so now that we have the leaderstats we need to make a system that allows the player to buy a tool using their cash.

First we will make a gui that has a purchase button.

image|300x300

ok so thats what i made, you dont have to make the exact same one. to make the script where the part opens the gui we need a proximity prompt and a local script.

Put the prompt into the part and the local script inside of the Frame of your gui -- make sure the frame isnt visible when the game starts--

inside the local script this is the code you will need.

local Frame = script.Parent
local Part = game:WaitForChild("Workspace").OpenParrt
local Prompt = Part.ProximityPrompt

Prompt.Triggered:Connect(function()
	Frame.Visible = true
end)
 

that was the basic open script, if you know more about scripting, you can use a IF, Then Function/ Argument to determine if the gui is open or not.

now in order to determine if the player has enough money, and what they are going to buy we are going to need to use math, and replicated storage.

inside of replicatedstorage create a folder and name it tools, inside the folder add your desired tool. inside of the buy button in your gui add a local script.

the code:

local player = game.Players.LocalPlayer
local Cash = player.leaderstats:FindFirstChild("Cash")
local Tool = game.ReplicatedStorage.Tools.TestTool

script.Parent.MouseButton1Click:Connect(function()
	if Cash.Value >= 100 then
		Cash.Value = Cash.Value - 100
		local ClonedTool = Tool:Clone()
		ClonedTool.Parent = player.Backpack
	end
end)

if you would like to understand what that just did, the script grabbed the cash value from the player and used Math operators to subtract from it as well as checking if the value was greater than or equal to the price!

This conludes how to make leaderstats purchase gui and tools! Happy scripting.

View in-game to comment, award, and more!