How to check if a player Dies/Resets or leaves the game mid function

For clarification, any function or ‘connection’ needs to have that table insert line?
But instead of having the event it would just be the standard function line, e.g.

		-- Go Away Sensor 
		table.insert(connections[Player], Sensor.Touched:Connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if sensoractive == true then	
				if player.Name ~= activeuser then	
					player.Character.HumanoidRootPart.CFrame = begone.CFrame
				end
			end
		end))

Any connection will need to be inserted into the connections table so that they’re disconnected properly when the player leaves the game, else you’ll have a memory leak. You can use this method to add connections to the table:

connections[Player][1] = Sensor.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if sensoractive == true then	
		if player.Name ~= activeuser then	
			player.Character.HumanoidRootPart.CFrame = begone.CFrame
		end
	end
end)

But I really don’t recommend it since it’s easy to accidentally overwrite a connection, which will cause it to never be disconnected

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.