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

How do I make a changed leaderstat appear for everyone?

Okay basically i'm just trying to make kind of a cookie clicker game.

I have a local script inside a button, which is inside a screen gui. The script has the following code inside it:

script.Parent.MouseButton1Click:Connect(function()

game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value + 1

wait(0.1)

end)

Basically this is supposed to give the player 1 more cash every time he clicks the button, which it does do that, but only the player who clicked it sees this, no one else does.

I'm assuming I have to use an event in order to make this work, however i'm not entirely sure how those work yet, and any help/explanations would be very appreciated!

Also, how do I organize my code in a way that people can understand? I have seen people get the numbers out beside but I don't know how to do that.

2 comments

0

Assuming your game is FE, this would be exploitable. You need to create and set the leaderstat values in the server and have the server change it. The server will replicate the value to other players, so there is no need for remote events. If you use GUIs for clicking, you have to use Remote Events to communicate with the server. Otherwise you can use ClickDetectors on the server.

0

Okay, thanks. I'll try to see what I can do.

1 answer

✓ Accepted answer

Hello, Alexanders1123, my name is TheOnlySmarts and I think I know the solution to your problem.

The reason why only the player who clicks sees it but others don't is because you're changing the Cash through the Client. Because this is inside of a TextButton, which is inside of StarterGui/PlayerGui, the game automatically registers it as a Client Script.

The client is the player who runs the script through their monitor. Which in shorter terms, means 'Local'.

However, if we were to change the Cash through the server, then the whole 'server' would see the Cash change.

But how would we do this?

Well, we will need the following...

  • RemoteEvent - place in ReplicatedStorage
  • Variables in our script

Now that we have listed the following things we will need, we can now move on to scripting.

-- LocalScript ( Inside of TextButton )

local remoteEvent = game.ReplicatedStorage.RemoteEvent
local value = game.Players.LocalPlayer.leaderstats.Cash

script.Parent.MouseButton1Click:Connect(function()
    remoteEvent:FireServer("changeValue", value)
end)

-- Script ( Inside of ServerScriptService)

local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(plr, changeValue, value)
    value.Value = value.Value + 1
end)

I hope this has helped you, if it did, let me know and please upvote if you can ;)

Thanks, TheOnlySmarts.

5 comments

0

Why are you passing a parameter that you never use?

0

The "changeValue" parameter, not the player parameter.

0

Works great, thank you! Just a small question, what does the changeValue mean in lines 7 and 14?

0

The changeValue parameter is just an easier way of identifying the event and also making it easier to access the event.

0

And also, no problem Alexanders, I hope you have fun when scripting ;D

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