I have a simple GUI with a button, which is supposed to increase a "cash" leaderstat by 5
cash = game.Players.LocalPlayer.leaderstats.Cash script.Parent.MouseButton1Click:Connect(function() cash.Value = cash.Value + 5 end)
The script is not local.
However when run it doesn't change the value. Not really sure what to do when it comes to these sorts of errors, any suggestions?
To start with, you have to create a RemoteEvent and place it in ReplicatedStorage. Name it what you want. We will use that in both our scripts. Then create a ServerScript in ServerScriptService. The script you have in your GuiButton has to be a LocalScript. This is the code we will write into the scripts:
LocalScript
local player = game.Players.LocalPlayer local remEvent = game.ReplicatedStorage.RemoteEvent -- Change this to the name of your RemoteEvent script.Parent.MouseButton1Click:Connect(function() remEvent:FireServer() -- Automatically, this function sends "player" as an argument, to tell the ServerScript which player who should get the money end)
ServerScript
local remEvent = game.ReplicatedStorage.RemoteEvent -- Change this to the name of your RemoteEvent remEvent.OnServerEvent:Connect(function(player) -- The ServerScript receives the argument local cash = player.leaderstats.Cash cash.Value = cash.Value + 5 end)
I hope this helps you, good luck!