Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

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

Asked by 9 years ago

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

Log in to vote
0
Answered by
DataStore 530 Moderation Voter
9 years ago

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.

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 whyOmustOitObeOme 7 — 9y
0
In the edit it's far more complicated than it needs to. EzraNehemiah_TF2 3552 — 9y
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. DataStore 530 — 9y
0
I edited my script. I just deleted one line. Happy? EzraNehemiah_TF2 3552 — 9y
Ad
Log in to vote
0
Answered by
iaz3 190
9 years ago

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


Log in to vote
-1
Answered by 9 years ago

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
1
If you read the comment he left on my post, you would realise that this isn't what he's looking for. DataStore 530 — 9y
0
@ DataStore , So? My answer would work also. Just put it in a loop. EzraNehemiah_TF2 3552 — 9y
1
@Dragon, "I just want to show every player that has their health below 100" - He didn't ask for you to heal them. DataStore 530 — 9y

Answer this question