Below is my script. The print inside the function works but the method itself returns nil, why is that?
local function requestPlotState()
local connection
connection = plotStateHandler.OnClientEvent:Connect(function(plotStateLol)
print(plotStateLol)
connection:Disconnect()
return plotStateLol
end)
plotStateHandler:FireServer()
end
print(requestPlotState())
You are returning plotStateLol in a different function. You need to return it in the requestPlotState function and not in the function connected to the remote function.
Like this:
local function requestPlotState()
local connection
local toReturn;
connection = plotStateHandler.OnClientEvent:Connect(function(plotStateLol)
print(plotStateLol)
connection:Disconnect()
toReturn = plotStateLol
end)
plotStateHandler:FireServer()
plotStateHandler.OnClientEvent:Wait()
while not toReturn do task.wait() end
return toReturn
end
print(requestPlotState())
No problem! I would also suggest instead of using remote events, this would be a perfect situation to use a RemoteFunction instead, as it will wait for the server to respond and is more performant than the code above.