I need help with my money leaderboard


I’m trying to make a cola game, but when you buy a lot of stuff, it gives you negative cash, but players can still buy stuff, but they will go in debt even more, I want to add it where, if you dont have enough money, it wont let you buy it.

this is the leaderboard script:

game.Players.PlayerAdded:connect(function(p)
local stats = Instance.new(“IntValue”)
stats.Name = “leaderstats”
stats.Parent = p

local money = Instance.new("IntValue")
money.Name = "Cash" 
money.Value = 50
money.Parent = stats
end)

and this is the spend money script:

script.Parent.ClickDetector.MouseClick:connect(function(player)
if player:FindFirstChild(“leaderstats”) then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 50 – price of the item
game.Lighting.Cheeto:Clone().Parent = player.StarterGear
game.Lighting.Cheeto:Clone().Parent = player.Backpack
end
end)

2 Likes

Try checking if the player has the amount of money required to buy the item.

1 Like

how do I do that, I’m not that good at scripting…
I can’t hire anyone because, I dont have money.

I cant really help because I don’t know how you setup your leaderboard.
Hopefully somthing like this should work

if yourLeaderboard.Value < 30 then
-- The player has more then 30 dollars.
-- Your code here
end

well this is the script I used for my leaderboard:

game.Players.PlayerAdded:connect(function(p)
local stats = Instance.new(“IntValue”)
stats.Name = “leaderstats”
stats.Parent = p

local money = Instance.new("IntValue")
money.Name = "Cash" 
money.Value = 50
money.Parent = stats
end)
1 Like

That’s fine! But what is the script that removes the money? This script only gives the player 50 cash upon execution.

this is the script:

script.Parent.ClickDetector.MouseClick:connect(function(player)
if player:FindFirstChild(“leaderstats”) then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 50 – price of the item
game.Lighting.Cheeto:Clone().Parent = player.StarterGear
game.Lighting.Cheeto:Clone().Parent = player.Backpack
end
end)

This should work, sorry im on mobile and I cannot test this in studio /:

local itemCost = 50 --This is how much the item costs
script.Parent.ClickDetector.MouseClick:connect(function(player)
if player:FindFirstChild(“leaderstats”) then

if player.leaderstats.Cash.Value >= itemCost then


player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - itemCost 
game.Lighting.Cheeto:Clone().Parent = player.StarterGear
game.Lighting.Cheeto:Clone().Parent = player.Backpack
end
end
end)

Edit: I made a mistake and did player.leaderstats.Value instead of player.leaderstats.Cash.Value
This is fixed now

1 Like

I tested it, and it didn’t work.

What didn’t work, was there an error? Or did it just do nothing?

did nothing… I look in breakpoints, for any errors and saw nothing

There are videos on YouTube about money for leader stats and how to lose money when you buy stuff.

Nope, it sadly doesn’t work…

Was there any errors in the output?

What? Mine is working its just that when I buy stuff thats over the money limit, it gives me negative.

local itemCost = 50 --This is how much the item costs
script.Parent.ClickDetector.MouseClick:connect(function(player)
	if player:FindFirstChild("leaderstats") then
		if player.leaderstats.Cash.Value > itemCost or player.leaderstats.Cash.Value == itemCost then
			player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - itemCost 
			game.Lighting.Cheeto:Clone().Parent = player.StarterGear
			game.Lighting.Cheeto:Clone().Parent = player.Backpack
		end
	end
end)

this should work @1_kly

1 Like

10:46:22.260 - Workspace.Map 1.Parts.Main.Script:3: Expected ‘then’ when parsing if statement, got ‘player’

yeah an error

Thank you! It actually works…

1 Like

the script works, but can you program where it doesn’t give you copies of the items.