Attempt to iterate over a RBXScriptConnection value

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

1 Like

First off, you’re looping twice. Second, you realize the issue here no?

1 Like

Yes I understand the error, and idk why i was looping twice, must have gone into autopilot when writing that, but how can I avoid triggering the error though?

1 Like
function module:Wait(remote, valueToCheck)
	pause = true
	local tableToPass = {}
	local waitRemote = remote.OnServerEvent:Connect(function(player, value, other1, other2, other3)
		if value == valueToCheck then
			pause = false
			table.insert(tableToPass, player)
			table.insert(tableToPass, value)
			table.insert(tableToPass, other1)
			table.insert(tableToPass, other2)
			table.insert(tableToPass, other3)
			return
		end
	end)
	while pause do
		task.wait(0.1)
	end
  waitRemote:Disconnect()
	return true, tableToPass
end
1 Like

You might’ve been wanting to loop through tableToPass?

1 Like

yeah, which is why i rewrote the script. No idea why i did it that way and why it took me like 2 hours to realise that i was being dumb. Sorry for wasting ur time and ty :slight_smile: