Need help with leaderstats

I am having problems with leaderstats. I made a button that gives you 1 level for 100 coins, and everytime you buy a level it becomes 25 coins more expensive.

This is my script:

local Price = 200


script.Parent.MouseButton1Click:Connect(function(plr)
	if plr.leaderstats.Coin < Price then return end
	plr.leaderstats.Level = plr.leaderstats.Level + 1
	plr.leaderstats.Coins = plr.leaderstats.Coin - Price
	Price = Price + 25
end)

It does nothing.

1 Like

You’ll need a RemoteEvent otherwise the stats will just be changed client-side and won’t reflect to the server.

I don’t know how to use those.

And the stats saving to the server isn’t the biggest problem right now.

local Price = 200

script.Parent.MouseButton1Click:Connect(function(plr)
	if plr:WaitForChild("leaderstats"):WaitForChild("Coin").Value < Price then
		return
	end
	plr:WaitForChild("leaderstats"):WaitForChild("Level").Value += 1
	plr:WaitForChild("leaderstats"):WaitForChild("Coins").Value -= Price
	Price += 25
end)

It still doesn’t do anything. :frowning:

Then you probably have some stuff setup incorrectly. Make sure the script is a local script & place it inside the button which is being clicked.

I don’t have a RemoteFunction as you mentioned in one of your comments.

And yes, it is a localscript

As said before In this case I would suggest for you to use a RemoteEvent because you are probably firing this through a localscript and it will change only on the client that means only one player will see what changed, the rest won’t.

If this is not really a problem for you, then here is the code.

local Price = 200
local Button = script.Parent

Button.MouseButton1Click:Connect(function(Player)
    if Player:WaitForChild("leaderstats").Coins.Value < Price then
       return nil
  else
       Player:WaitForChild("leaderstats").Level.Value += 1
       Player:WaitForChild("leaderstats").Coins.Value -= Price
       Price += 25
    end
end)

Check the edit, it may work now.

Nope, it still doesn’t work sadly.

You’ve likely got something setup wrong then (this is working my end), can you share the leaderstats script & a screenshot of how the ScreenGui is organised?

if plr:WaitForChild("leaderstats"):WaitForChild("Coin") < Price then
		return

looks like if player have less coins then the price he will level up coin < price

how Coins - price if player have less coins then price ? the coins will be under 0

I’ve made another edit, try again please. That edit will work if you want to keep everything local. I accidentally forgot to fetch the value in the conditional statement. I’m going to provide a server-wide solution in my next reply too.

The following will be if you want the leaderstats to be reflected to the entire server:

– SERVER SCRIPT –

local players = game:GetService("Players")
local replStorage = game:GetService("ReplicatedStorage")
local levelEvent = replStorage:WaitForChild("LevelEvent")

players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	local levelStat = Instance.new("IntValue")
	levelStat.Parent = leaderstats
	levelStat.Parent = 0
	local coinsStat = Instance.new("IntValue")
	coinsStat.Parent = leaderstats
	coinsStat.Value = 0
end)

levelEvent.OnServerEvent:Connect(function(player, Price)
	player:WaitForChild("leaderstats"):WaitForChild("Level").Value += 1
	player:WaitForChild("leaderstats"):WaitForChild("Coins").Value -= Price
end)

The server script includes leaderstats itself.

– LOCAL SCRIPT –

local Price = 200
local replStorage = game:GetService("ReplicatedStorage")
local levelEvent = replStorage:WaitForChild("LevelEvent")

script.Parent.MouseButton1Click:Connect(function(plr)
	if plr:WaitForChild("leaderstats"):WaitForChild("Level").Value >= Price then
		levelEvent:FireServer(Price)
		Price += 25
	end
end)

Organisation:

image

I’ll try this. Thank you for dedicating your time to helping me

it will error if you change the value of leaderstats in local scripts.

Alternatively, you can just use:

local Price = 200

script.Parent.MouseButton1Click:Connect(function(plr)
	if plr:WaitForChild("leaderstats"):WaitForChild("Coin").Value < Price then
		return
	end
	plr:WaitForChild("leaderstats"):WaitForChild("Level").Value += 1
	plr:WaitForChild("leaderstats"):WaitForChild("Coins").Value -= Price
	Price += 25
end)

If everything being localised is fine.