Counting not counting correctly

so i have a script that counts of players inside of a table called alive and if there is 0 then all players get teleported to the spawm location code
– countng fuction

local function Check2(tab)
	local num = 0
	for i= 1, #tab do
		num += 1
	end
	return num
end

– checking function

for i = 1, Roatation do
	if Check2(Alive) == 0 then
				GivenPoints = true
				print("G2")
				for i = 1, #Finshed  do
					CalulateTickets(i)
				end
				break
				end
			ColourPickerSerivce.Run()
			task.wait(3)
		end 

but for some reason if there is one player lefted the counter wont count it and they and all of the players get teleported to the spawn location

1 Like

You can get the number of elements in a table using #tablename
It saves having to manually count them

1 Like

i had it like that before but it did the same thing

1 Like

Are you storing it inside a key-value table instead of an array? If so, then you could just use

local count = 0
for _ in pairs(your_table) do
    count+=1
end
return count

The # operator only works properly in arrays.

1 Like

it still teleports both the players whe only one is dead

The part of the code which is the problem could’ve been in a different place then. What’s the code which handles registering the players into the table?

this is

local function AddPlayersToAliveTabel()
	print("Adding People To The Alive Table")
	Status = "InProgess"
	local AmoutPutIn = 0
	for i, plr in ipairs(game.Players:GetPlayers()) do
		table.insert(Alive,plr.UserId)
		AmoutPutIn += 1
	end
	print("AddIng"..AmoutPutIn)
	return
end

1 Like

Do you ever remove dead players from the array?

yes in my functions when they die or reach the finsh line it removes them from the array

when they die

			table.remove(Alive, table.find(Alive, plr.UserId))

they reach the finsh line

				table.remove(Alive, table.find(Alive, plr.UserId))

also i have it print if they die and

image

g2 means if finsh but only one person died it prints twice

1 Like

i firgue i had to to rescript a bit because the counting is just broken so i had to put a tag on a player and if they die they lose it and if they finsh they lose it

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