I tried to make a stat giver but when i click the GUI It doesn't give me the stat! here is what i scripted that wont work for some reason
local Remote = game.ReplicatedStorage:WaitForChild("giveCash") Remote.OnServerEvent:Connect(function(stat) game.Players.LocalPlayer.leaderstats.Money= game.Players.LocalPlayer.leaderstats.Money+stat print("works") end)
But it doesnt add a stat for me! If you know how to fix this please let me know!
You cannot use RemoteEvents for this sadly. You can only use a script. This is due to the fact that a LocalScript makes it so that the rest of the players in the game cant see your stat change.
If you want to do this and have it do the same function, use this script.
funtion GiveMoney(amount) for i,plr in pairs(game.Players:GetPlayers()) do local Value = plr.leaderstats:FindFirstChild("Money") Value.Value = Value.Value + amount end end GiveMoney(15)--Gives every player 15 money
You had many errors with your original script:
-You needed to add a .Value to the end because the IntValue is an object
-The script was local, making it so the server or game, can't see the changes
-OnServerEvent is called from the SERVER, not the CLIENT
If you have any questions, you can comment below! I hope you enjoy this script! Bye now!
Best,
TheLastHabanero
The other answer only had it so you get Cash locally, so only you see the difference. If you wanted to fix this, here's what you would do:
First, as the other answer mentioned, please make this a ServerScript.
Second, game.Players.LocalPlayer
does not work on ServerScripts. Think of it like this: you're trying to get a LocalPlayer from the server, which handles all players. How will it know which one is local?
Third. This is a big one. When you use ‘.OnServerEvent`, the first argument is the player who fired it. An example:
Event.OnServerEvent:Connect(function(player, otherStuff) print(player.Name); -- this will print their Username. player is the PlayerInstance. end)
Player.Money
. I'm assuming Money
is a NumberValue, so you'll need to add a .Value
to the end of it.To fix your script, you should change it to a ServerScript and do the following:
local Remote = game.ReplicatedStorage:WaitForChild("giveCash") Remote.OnServerEvent:Connect(function(plr, stat) ple.leaderstats.Money.Value = plr.leaderstats.Money.Value + stat print("works") end)
Also, never trust the client. You should handle adding and subtracting cash on the server.