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

How to change leaderstat from script?

Asked by 4 years ago

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?

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. Spjureeedd 385 — 4y
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 Spjureeedd 385 — 4y
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. KroKey 0 — 4y
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. Dylan011444 59 — 4y
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 ForeverBrown 356 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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!

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). ForeverBrown 356 — 4y
0
@ForeverBrown I think that is only when you do :FireClient() Spjureeedd 385 — 4y
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. karlo_tr10 1233 — 4y
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 karlo_tr10 1233 — 4y
0
Okay thank you, I might have known that once.. Spjureeedd 385 — 4y
Ad

Answer this question