Help with money script

Have you checked for errors and did you replace the full script?

yes i looked over it and everything and also replaced it

I would also suggest cloning the tools after the player’s money has been confirmed to be enough to purchase the item.

1 Like

Try this code

local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("EquipRe")

local function giveTool(player, item, price) -- Make a function
	local money = player.leaderstats.Money

	if money.Value >= price then -- If the money is greater or equal to the price then run other code
		money.Value -= price -- Removing the player's money.

		local tool = rs:WaitForChild(item):Clone() -- Starter gear tool
		local tool1 = rs:WaitForChild(item):Clone() -- Backpack tool

		tool.Parent = player.StarterGear -- Give the player startergear tool so if they die they get it back
		tool1.Parent = player.Backpack -- Then put the tool in backpack so player doesn't need to reset to get it
	end
end

re.OnServerEvent:Connect(giveTool)

i have no idea what you mean by " after the player’s money has been confirmed to be enough to purchase the item."

He means by where it says if money.Value >= price then

1 Like
local rs = game:GetService("ReplicatedStorage")

As the variable is used multiple times.

As I said earlier you need to define rs.

Use the code I sent above

local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("EquipRe")

local function giveTool(player, item, price) -- Make a function
	local money = player.leaderstats.Money

	if money.Value >= price then -- If the money is greater or equal to the price then run other code
		money.Value -= price -- Removing the player's money.

		local tool = rs:WaitForChild(item):Clone() -- Starter gear tool
		local tool1 = rs:WaitForChild(item):Clone() -- Backpack tool

		tool.Parent = player.StarterGear -- Give the player startergear tool so if they die they get it back
		tool1.Parent = player.Backpack -- Then put the tool in backpack so player doesn't need to reset to get it
	end
end

re.OnServerEvent:Connect(giveTool)

ok thank you that one did worked

If your problem is solved then mark the post as a solution.

There is still a massive security flaw with this script. You should never send things like prices via Remotes, because it can be easily altered and exploited. For example, let’s say I want to buy an item which costs $100,000, and I send $0 as the price, then I’m getting that item for free!

To fix this, we simply remove the price parameter and do that check on the server. You should have a big list of all the prices saved somewhere other than the client. So, taking your script, here is what it would look like:

local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("EquipRe")
local prices = {
    ["Tool1"] = 100,
    ["Tool2"] = 300 -- and so on, you get the point
}

local function giveTool(player, item) -- Make a function - Notice, I removed "price"
	local money = player.leaderstats.Money

	if money.Value >= prices[item] then -- If the money is greater or equal to the price then run other code - We compare with the price the SERVER has
		money.Value -= prices[item] -- Removing the player's money.

		local tool = rs:WaitForChild(item):Clone() -- Starter gear tool
		local tool1 = rs:WaitForChild(item):Clone() -- Backpack tool

		tool.Parent = player.StarterGear -- Give the player startergear tool so if they die they get it back
		tool1.Parent = player.Backpack -- Then put the tool in backpack so player doesn't need to reset to get it
	end
end

re.OnServerEvent:Connect(giveTool)
1 Like

now its not giving me the item