Can't get table from server for some reason

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())
1 Like

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())
1 Like

I tried that earlier, but now I realize I wasn’t waiting until returnMe was not nil. It works now, thank you for your help :slight_smile:

1 Like

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.