Comparing Player Health

I am trying to compare Player Health to players that are in game, and check if they are the same, but I can’t figure out how to do it.

	local AliveTable = {}
		for i, v in pairs(game:GetService("Players"):GetChildren()) do
			local Var = v.Character:FindFirstChild("InGame") ---Ignore this
			if Var.Value == true then
				AliveTable[v.Name] = v.Character.Humanoid.Health
			end
		end
		for i = 1, #AliveTable do
			
		end

Compare their health to what? Are you trying to get all the players who are currently alive?

compare health to each player that is in game (as in a round)

local alive = {}
for _, player in pairs(game.Players:GetPlayers()) do
    if player.InGame.Value and player.Character and player.Character.Humanoid.Health > 0 then
        table.insert(alive, player)
    end
end

You should have the InGame value in the player, and I recommend setting to false immediatly after they die and respawn.

I already have the InGame value, so I will incorporate a table that would have the people that are alive

I’m not sure if this is causing the problem because it isn’t clear “what is not working” but one thing that confused me starting out with Lua is that while a table acts like an array sometimes, it acts like a dictionary other times and then you can’t treat it like an array.

What does that mean? Basically, doing this #AliveTable to a table acting like an array will return the correct count of elements. But doing the same thing to a table acting like a dictionary will not!

For a dictionary you need something like this

function getTableLength(t)
	local count = 0
	for _ in pairs(t) do 
		count = count + 1 
	end
	return count
end

Example of “table as array”:

{ "this", "is", "array" }`

Example of table as dictionary:

{ ["this"] = 1, ["is"] = 2, ["dictionary"] = 3 }

The behavior of the # operator then makes sense when reading the docs… see here

2.5.5 – The Length Operator

The length operator is denoted by the unary operator # . The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte).

The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil ; moreover, if t[1] is nil , n can be zero. For a regular array, with non-nil values from 1 to a given n , its length is exactly that n , the index of its last value. If the array has “holes” (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).

Dictionaries have “holes”. Arrays do not.

If you use

for i = 1, getTableLength(AliveTable) do
...
end

It may work as you’re expecting.

Just suggesting that you move it to the player object, but it should work either way.

I meant to say that I was trying to use dictionary, I am not trying to get table length, but trying to compare data in a dictionary to see if two or more players have the same amount of health

Right. And what I said is it likely won’t work as you’ve written it :slight_smile: specifically, this #AliveTable won’t work. Read my post for why.

Why, exactly? I don’t see a use case for this.

then how should I do it instead of using a dictionary, with also keeping track of player name to the health while comparing?

Again, this is in my post…

for i = 1, getTableLength(AliveTable) do
...
end

where getTableLength =

local function getTableLength(t)
	local count = 0
	for _ in pairs(t) do 
		count = count + 1 
	end
	return count
end

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