Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you add 1 + to a leaderstats?

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.

1 answer

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)
    

6 comments

0

this isnt working for me i used your code by combining both parts and i used them in a script in a TextButton in starterGUI, yet it does nothing.(i did not move leaderboard script out of serverscript service.)

0

also i cant get the leaderboard to save, i used a auto save script by climaximus

0

Poor solution.

0

I'm sorry everyone; when I wrote this I forgot that the player object didn't have to be found again with a loop as that's inefficient. Also, I should've described how the RemoteEvents work further. Sorry about that.

0

I can make an answer if you'd like.

0

@spot6003 please help by making a answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.