How do I send variable values through remote events?

So I’m kind of new at scripting (like 5 - 7 months of experience) and I need to use a remote event for a fall damage script. What I’m trying to do is send the amount of damage taken from the client to the server.

Here’s an example:

  • local script
local damage = math.random(30,60)
remoteevent:FireServer(damage)
  • server script
remoteevent.OnServerEvent:Connect(function(player, damage)
player.Character.Humanoid.Health -= damage
end)

I was trying something similar to this but I realized that’s not how remote events work, I have tried to search for help and I can’t figure out how to send a variable value through a remote event from the client to the server, can someone please explain it to me?

1 Like

You should not be telling the server what the damage should be in any instance, ever.
That opens you up to exploit vulnerabilties.

Otherwise, I don’t see how that isn’t how remotes work. Looks normal to me for the most part.

What do you mean “sending a variable value?” In my experience thats exactly how you would do it

edit: obviously what @Synteuro said as well
If you are going to do that then check that the damage is a positive value so the client can’t deal negative damage to themselves

Yes, I know my game is vulnerable to exploiters but I’ll get into that later and do some research

Then I don’t see why it doesn’t work.

Ok so, while I was replying to your post I was kind of remaking the script and it works now, it’s fixed now. I don’t know what the error was though.

Thanks for the help of you both!

It’s possible you were firing the server before the “.OnServerEvent” event listener was properly hooked on the listening side. Take the following for example.

--CLIENT
game.ReplicatedStorage.RemoteEvent:FireServer()

--SERVER
task.wait(10)
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player)
	print(Player)
end)

The local script fires the server as soon as it executes but the server script hasn’t hooked the corresponding “.OnServerEvent” event until 10 seconds have elapsed since the script’s execution. This means that the event is never fired.

I think the problem was that I was sending more than one argument through the remote event, I don’t know if you can actually do that

You can send as many as you like.

1 Like

I think its because the server thinks what you defined as “player” is the “damage” you sent from the client

I could be wrong though
try

remoteevent:FireServer(player, damage)

That’s not it. Server always knows from who is the message. Sending a player would make no sense. It would make sense only in cases when player does an interaction with other player, for example sending a party request or this kind of stuff.

1 Like

I’m having issues sending values through my Event:FireServer(value) setup as well.
I’m catching the event fire in my server script, but it won’t catch or show me the values it should have.

The location of the scripts is important aswell, the Serverscript should be in ServerScriptService and the :FireServer can be in the PlayerGui, StarterPlayerScripts or in a Tool

I ended up fixing it. I had it in the right places, but I was trying to make a local variable with a local asset that didn’t exist server side which broke the whole script.