I made this function as a general use function to wait for certain events to happen (context: single player story-like game, so need to frequently wait for player to complete an action to move forwards) and I then need to pass any arguments from the remote I was waiting for, if the first value is a specific value, but also then be able to disconnect the function afterwards. However it throws this error:
ServerScriptService.Main.Timing:20: attempt to iterate over a RBXScriptConnection value
How can I manage to both be able to have this self-contained function and be able to pass on the arguments from it to whatever script called it, while being able to disconnect the nested function
function module:Wait(remote, valueToCheck)
pause = true
local waitRemote = remote.OnServerEvent:Connect(function(player, value, other1, other2, other3)
if value == valueToCheck then
pause = false
return {player, value, other1, other2, other3}
end
end)
while pause do
task.wait(0.1)
end
local tableToPass = {}
for i,v in waitRemote do
for i,v in waitRemote do
if v ~= nil then
table.insert(tableToPass, v)
end
end
end
waitRemote:Disconnect()
return true, tableToPass
end