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

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

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

✓ Accepted answer

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

2 comments

0

This answer is not quite accurate. It's a good answer, but it's only local, and won't save.

0

Thanks!

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.

4 comments

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.

0

Yes, that is why I mentioned to never trust the client.

0

gee! thank you

0

I came back here because i just now realized that exploiters can use some remote hack and fire it through them

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