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)
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)
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?
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)