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

How to change leaderstat from script?

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?

5 comments

0

You say the script is not Local. You can only use "LocalPlayer" in a LocalScript so that line doesn't work. And I don't know if only LocalScripts work in GUIs but I highly recommend it. So you have to change it to a LocalScript. But if you want the stats to change then it has to be a ServerScript that does it.

0

I can't answer right now, I can answer in like an hour, but I believe someone else will do it before then. But I do have an answer for your problem

0

What the person above says is correct, you would have to use a local script and then fire a remote event to a server script, which is where you will have to change the value.

0

If you really want to use a Script instead of a LocalScript, you could as well do "Player = script.Parent.Parent.." until you reach the player. That's how I sometimes do it.

0

Do not use scripts on the client. Look over this article to understand how the client-server model connects https://developer.roblox.com/en-us/articles/Roblox-Client-Server-Model

1 answer

✓ Accepted answer

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!

5 comments

0

When using :FireServer() you do not have to send a player argument, it will do so automatically. The first parameter in the OnServerEvent event will always be the player(automatically).

0

@ForeverBrown I think that is only when you do :FireClient()

0

@ForeverBrown is right, first argument when using :FireServer() will always be player, FireClient() requires player to be sent but doesnt have it as argument when received by client as that player is actually the target.

0

Also in your case when you sent player it would actually mess up other arguments if you had more than 1 as it would count player as second argument

0

Okay thank you, I might have known that once..

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