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

My RemoteEvents are not working! How do i fix them?

Asked by 4 years ago

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!

2 answers

Log in to vote
0
Answered by
haba_nero 386 Moderation Voter
4 years ago

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

0
This answer is not quite accurate. It's a good answer, but it's only local, and won't save. GeneratedScript 740 — 4y
0
Thanks! Actaully_Mine 6 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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)
  • Fourth, you're using 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.

1
players can also fire the remote event whenever they want so be careful. make checks on the server to make sure that the money is being given validly. royaltoe 5144 — 4y
0
Yes, that is why I mentioned to never trust the client. GeneratedScript 740 — 4y
0
gee! thank you Actaully_Mine 6 — 4y
0
I came back here because i just now realized that exploiters can use some remote hack and fire it through them Actaully_Mine 6 — 4y

Answer this question