I have a leaderboard script: `local dataStoreService = game:GetService("DataStoreService") --Service for DataStore
local cashDataStore = dataStoreService:GetDataStore("cashDataStore")--DataStore for Cash
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue",leaderstats)
cash.Name = "Cash"
local data
local success,errormessage = pcall(function()
data = cashDataStore:GetAsync(player.UserId.."-Cash")
end)
if success then
cash.Value = data
else
print("There was an error while getting data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success,errormessage = pcall(function()
cashDataStore:SetAsync(player.UserId.."-Cash",player.leaderstats.Cash.Value)
end)
if success then
print("Data successfully saved!")
else
print("There was an error when saving data!")
warn(errormessage)
end)` and i am trying to add 1+ value to cash after clicking a gui button.
The client can't modify leaderboard values as only the server can manage that. To solve this, use a RemoteEvent in the client to fire a function to the server when a player clicks a button.
-- Local script local ReplicatedStorage = game:GetService("ReplicatedStorage") local Event = ReplicatedStorage:WaitForChild("RemoteEvent") script.Parent.MouseButton1Click:Connect(function() Event:FireServer() end)
Also on the server side of things, you need to first get the previous value of the stat, and then add 1 to it.
-- Server script local ReplicatedStorage = game:GetService("ReplicatedStorage") local Players = game:GetService("Players") local Event = Instance.new("RemoteEvent") Event.Parent = ReplicatedStorage Event.OnServerEvent:Connect(function(player) player.leaderstats.Gold.Value = plr.leaderstats.gold.Value + 1 end)