Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you add 1 + to a leaderstats?

Asked by 5 years ago
Edited 5 years ago
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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
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.) ChaoticPierce 2 — 5y
0
also i cant get the leaderboard to save, i used a auto save script by climaximus ChaoticPierce 2 — 5y
0
Poor solution. User#24403 69 — 5y
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. DirectorDexter 69 — 5y
View all comments (2 more)
0
I can make an answer if you'd like. spot6003 64 — 5y
0
@spot6003 please help by making a answer. ChaoticPierce 2 — 5y
Ad

Answer this question