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 return a value from an event:connect(function() end)?

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

✓ Accepted answer

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)

1 comment

0

Thank you!

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