Issue with tables?

I have an issue with my game where if one player leaves when there is 3 or more the entire system breaks (I’ve tried adding a players.playerremoving) but it wont work for some reason. The issue is caused by still having 3 or more players in the playing table but whenever the player leaves it is unable to give them the item.

RunService.Heartbeat:Connect(function()
	players.PlayerRemoving:Connect(function(leftplayer)
		print("PLAYER REMOVED")
		if table.find(PlayingPlayers, leftplayer) then
		print("FOUND PLAYER")
		table.remove(PlayingPlayers, leftplayer)
		end
	end)
	people = players:GetPlayers()
	for i, player in PlayingPlayers do
		local char = player.Character
		local humanoid = char.Humanoid
		local humanoidRootPart = char.HumanoidRootPart

		if humanoid.Health <= 0 then
			table.remove(PlayingPlayers, i)
			print(PlayingPlayers)
		end
	end
	
if #people < 2 then
	game.ReplicatedStorage.PlayerLeft:FireAllClients(PlayingPlayers)
	else
	
		if gameActive == false then
			gameActive = true
			Intermission()
			StartRound()
		end
	end
end)

this script constantly gives the error (only when someone leaves) “attempt to index nil with humanoid”

1 Like

Its an array.
Table.find returns a needle so do smth like:

local needle:number? = table.find(PlayingPlayers, leftplayer)
if needle~=nil then
table.remove(PlayingPlayers, needle)
end

Anyway bro wth why are you connecting connections every heartbeat?
What is wrong with you?

2 Likes

first problem i see with this is that youre creating a connection every frame, so youre running the same code really many times when someone leaves

1 Like

I was just unsure on where to put it cause i had tried it outside the function and nothing happened so i tried something new

Thank you for helping me, I’m sorry for putting the function inside the heartbeat but I just realized this is a script from when I left yesterday and was trying to try different things to see if it would work. I have it working now.

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