Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I use a "for" loop AND select a specific number of a table inside the loop?

In other words, I need to have a "for" loop and inside that loop be able to select a specific part of the table, i.e. "table[1]"

Here's an example of code that would cause an error but would be what I want:

local players = game.Players:GetChildren()

for _, players in ipairs(players) do
	if players.Character.Humanoid.Health < 100 then
		players[1].Character.Humanoid.Health = 100
	end
end

The output would read: ":5: 1 is not a valid member of Player

Is there a way to have a "for" loop and be able to specify a part of a table inside of a "for" loop?

3 answers

Edit: You didn't really explain what you wanted in your original post, meaning it just sounded like you wanted to get the first played given in the table given by the ':GetChildren()' (or GetPlayers) method of the Players service. Upon your given comment (to this answer), if you wanted to do what you've asked you could do the following: (This is based upon the extra information you gave in a comment to this answer, where you asked to "show every player that has their health below 100")

function GetBelowHundred()
	local Players = {}
	for _, Player in ipairs(game.Players:GetPlayers()) do
		if Player.Character ~= nil and Player.Character.Humanoid.Health < 100 then
			Players[#Players +1] = Player 
		end
	end
	return Players
end

local PlayersBelowHundred = GetBelowHundred() 

What I've done in the above code block is iterate through all the players, if their character isn't nil and their health is below 100 then their player object will be added to the 'Players' table. This table is then returned by the function, enabling you to do anything with the players whose health was below 100 when the function was called.

4 comments

0

This kind of works, but when you use the line that has "Players[1]", it shows every player in game.Players, when I just want it to show every player that has their health below 100. In other words, using "Players[1]" would show the very first player in the game, but I want the script to show the very first player to have their health under 100; it may or may not be the actual first player in the s

0

In the edit it's far more complicated than it needs to.

1

Dragon, read what he said. He makes NO mention of wanting to heal the player, rather that he wants to find a particular player who has LESS THAN 100 health. Do learn to read his comment good sir.

0

I edited my script. I just deleted one line. Happy?

Is this what you are looking for? based on your script, with minimal changes, this will:

1) go through all players ingame

2) check their health for below 100

3) do whatever you want, and break at the first player the script finds that meets the condition.

You don't need to select anything from a table to do this, and if you did make a table using all players found with health below 100, the first one in the table would still be the first one the script found, so this works just as well.

local players = game.Players:GetChildren()

for _, plr in ipairs(players) do
	if plr.Character.Humanoid.Health < 100 then
		plr.Character.Humanoid.Health = 100
		break -- This will exit the loop the first time it gets here.
	end
end


Well, DataStores is correct, but his script isn't.

local Players = game.Players:GetPlayers()

for _, Player in ipairs(Players) do
    if Player.Character ~= nil and Player.Character.Humanoid.Health < 100 then
        Players[1].Character.Humanoid.Health = 100 --The error is right here. See it?
    end
end

Yes, in the script it says Players[1]. Which means that the players 1st player in alphabetical order gets healed.


So I edited like this:

local Players = game.Players:GetPlayers()

for _, player in ipairs(Players) do
    if player:FindFirstChild("Character") and Player.Character.Humanoid.Health < MaxHealth then
        player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth --since player is a player. This would make all the players healed though.
    end
end

Just remember to put this in a loop or function!


Done! I hope it helps.


Fine, if you don't want them to heal, IT'S STILL SIMPLE.

local Players = game.Players:GetPlayers()

for _, player in ipairs(Players) do
    if player:FindFirstChild("Character") and Player.Character.Humanoid.Health < MaxHealth then
        print(player.Name) --Something.
    end
end

3 comments

1

If you read the comment he left on my post, you would realise that this isn't what he's looking for.

0

@ DataStore , So? My answer would work also. Just put it in a loop.

1

@Dragon, "I just want to show every player that has their health below 100" - He didn't ask for you to heal them.

This discussion is preserved as it was. New answers, comments, and votes are closed.