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

How do i get variable through remote event?

Asked by 1 year ago

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

Log in to vote
0
Answered by 1 year ago

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)
0
for your code i'd say you just need to add "rng" as a parameter in your Print() function TheDude646 72 — 1y
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? AgentViteC 58 — 1y
0
For your last comment, i know it, i just write a example because i am too lazy write my all code. AgentViteC 58 — 1y
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) TheDude646 72 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

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.

Answer this question