Can't compare number in table to a number

This script doesn’t, work. What did I do wrong? lives = 3, but it doesn’t do lives > 1

local players = {}

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			if #players > 0 then
				for i, v in pairs(players) do
					if v[plr.Name] then
						local lives = v[plr.Name]
						
						if lives > 1 then
							lives = lives - 1
						else
							if plr.TeamColor == game.Teams.Alive.TeamColor then
								plr.TeamColor = game.Teams.Lobby.TeamColor
							end
						end
					end
				end
			else
				table.insert(players, {[plr.Name] = 3}) -- Amount of lives
			end
		end)
	end)
end)
1 Like

you are inserting a table into a table with this line:

table.insert(players, {[plr.Name] = 3})

you can change it into this, though:

players[plr.Name] = 3

and also you would have to change these lines to make it look for the player name:

if v[plr.Name] then
local lives = v[plr.Name]
to
if i == plr.Name then
local lives = v

I tried that, but it still doesn’t work

does it error somewhere? or maybe try printing the table so we can see if the plr actually exists in it

it doesn’t go past if i == plr.Name then, and no, it doesn’t error anything

so perhaps the table is empty?

try printing the table before that line and send the output

v[plr.Name] = v[plr.Name] - 1

Because you minus the value from another variable

The table is empty, so the problem is players[plr.Name] = 3

I is the index of the player, it also doesn’t have any reason to have a check at all. You don’t need to use table.insert, instead I would recommend

players[plr.Name] = 3

This is because table.insert() inserts it like an array, if you’re creating a dictionary/object on the other hand, indexing directly works just fine. It also deletes the need entirely to have a loop at all. Instead you can just get the index, which is the player’s name and it’ll work perfectly.

then the code doesn’t get to this line in the first place:

players[plr.Name] = 3

so instead, remove it and check if the plr exists in the table before for i, v in pairs

My bad, it does print the table. This is the table after dying once:
image
After dying twice, it’s still the same

i honestly cant figure out why it doesnt continue after
if i == plr.Name then

since they should be the same, so could you also print what i and v are?

I can’t print it, it doesn’t go past if #players > 0 then, I’m really confused now

oh, the table can’t have something in it when the code can’t get to the line where it insert something into it.

is that line necessary for something? if not, you can just remove it.

Your table now uses strings instead of integers as it’s index.

#players will always return 0 in this case.

You don’t need the loop. You just need to directly check if players[plr.Name] exists or not. If it does, reduce the count, if it doesn’t, initialise it to 3.

I removed it, and print(i, v) is Player2 3

i just realized that you would also have to update the table. so you can replace

lives = lives - 1

with

players[i] -= 1

I tried that and it still doesn’t work

It’s important to understand why this didn’t work, and the difference between arrays and dictionaries, hence my reply here that seems to have been ignored.

However, I think you are overcomplicating it for yourself and dealing with a table (with potential memory leaks if you don’t remove players’ entries) is not necessary at all.

Try using Attributes, as shown in the example below:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			local lives = plr:GetAttribute( 'Lives' )
			if lives then
				-- Take a life away
				lives -= 1
				plr:SetAttribute( 'Lives', lives )

				-- Check if any lives remain
				if lives < 1 then
					-- No lives left
					if plr.TeamColor == game.Teams.Alive.TeamColor then
						plr.TeamColor = game.Teams.Lobby.TeamColor
					end
				end
			else
				-- Initialise the lives
				plr:SetAttribute( 'Lives', 3 )
			end
		end)
	end)
end)
3 Likes