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

How would I change a leaderstat value through a local script?

I want to change a leaderstat value via a localscript, however, local scripts only change things through client-sided and I need to change it through the serverside so that the data store would work. Would I need to change my data store code or my leaderstat addition code in order to send the updated value to the server side? Here is how I've done it so far:

Code:

player.leaderstats["Races won"].Value = player.leaderstats["Races won"].Value + 1

2 answers

✓ Accepted answer

You can't have a LocalScript change the leaderstats value. You have to do it via remotes. For this I think it's going to be relatively easy but we need a few things:

  • A RemoteEvent
  • A localscript sending the remote event data
  • A server-side script receiving the remote event data

First make a RemoteEvent in ReplicatedStorage and name it RacesWon. That's is going to enable communication between the server and the clients.

Next step we are going to make the server script receive the data and update the player's leaderstats:

local Remote = game.ReplicatedStorage.RacesWon

local function ServerEvent(player) -- the player which is telling the server to communicate
	local RacesWonStat = player.leaderstats:WaitForChild("Races won")
	RacesWonStat.Value = RacesWonStat.Value + 1
end

Remote.OnServerEvent:Connect(ServerEvent)
-- whenever the client Fires the remote event, function ServerEvent will execute.

All that is saying is basically if the remote event got something from the client, the server code will execute. Think of it as phone calls and operators.

Next step we are going to make the client code which is easier. All you need to do is get a variable of your Remote Event and fire it whenever you want to:

local Remote = game.ReplicatedStorage.RacesWon
Remote:FireServer() -- telling the server to execute the code

Your all done here are some links:

https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events https://developer.roblox.com/en-us/articles/Roblox-Client-Server-Model

2 comments

0

Thanks for helping and the added explanation, I knew it had something to do with this just didn't exactly know how to do it. Great explanation.

0

Thank you!

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