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

How do i get variable through remote event?

I was spended like 20 minutes to search for an answer, but result didn't appear. You can see EXAMPLE code, that means i dont use it in game.

--Client script--
remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
rng = math.random(0,100) --Defining variable
remote:FireServer(rng) --Firing this with variable
--Server script--
remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
function Print()
print(rng) --Using that variable, also, there saying that this is unknown global
end
remote.OnServerEvent:Connect(Print) --Connecting script

If you try to run this code, you will get "nil". I hope you understand my issue.

2 answers

✓ Accepted answer

you can pass it along as a parameter, for example:

local script:

game.ReplicatedStorage.exampleRemoteEvent:FireServer(exampleVariable)

server script:

game.ReplicatedStorage.exampleRemoteEvent.OnServerEvent:Connect(function(exampleVariable)
-- your code here
end)

4 comments

0

for your code i'd say you just need to add "rng" as a parameter in your Print() function

0

I still don't understand. Why in local script there is ExampleVariable, but in server there is just "exampl"? Also, why is there not defined this variable?

0

For your last comment, i know it, i just write a example because i am too lazy write my all code.

0

the code is too long for the comment to display and it cuts it off for some reason, the full line of code is: game.ReplicatedStorage.exampleRemoteEvent.OnServerEvent:Connect(function(exampleVariable)

That's only because you're not defining a parameter named rng within the function. FireServer(), the function you're trying to call, passes (but does not require) a minimum of one value:

  • The player that fired the event to the server.
  • (Optional, but often used) Any parameters to pass to the server.

In a sense, when you're doing this:

remote:FireServer(rng)

OnServerEvent will receive:

function Print(player, rng)
	-- code
end

You haven't defined any variables in your function, so rng is nil.

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