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

How do I return a value from an event:connect(function() end)?

Asked by
Nymint 85
10 years ago

Well I have a RemoteEvent, the server sends a table variable with objects to every client.

Then I have a Localscript for each client using .OnClientEvent on the RemoteEvent so they can get the table.

This is what I did to solve it,

--LocalScript
local Event=Game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
Event.OnClientEvent:connect(function(ObjectTable)
Data=ObjectTable
end)

I don't like using Global variables however, I personally don't feel comfortable using them... Is there a another way so the variable can be returned via function connected to the event or something? Important: This variable updates every single second

1 answer

Log in to vote
1
Answered by
jakedies 315 Trusted Moderation Voter Administrator Community Moderator
10 years ago

You could use a RemoteFunction to return a value (or values). But if you want to use RemoteEvents, you can just always declare Data as a local variable before hand so when the event is invoked, you can just set it.

--LocalScript
local Event=Game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local Data;
Event.OnClientEvent:connect(function(ObjectTable)
Data=ObjectTable
end)
0
Thank you! Nymint 85 — 10y
Ad

Answer this question