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

Help with i,v in pairs?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

I'm trying to get all players and make their head transparency = to 1

for i,v in pairs {game.Players} do
    v.Character.Head.Transparency = 1
end

4 answers

Log in to vote
1
Answered by 8 years ago

Like drahsid5 said, you made it a table. However, his script isn't that sufficient(I think that's the proper word, I'm tired ok?). You could replace GetChildren with GetPlayers, so if there's something like a Part in Players(not sure why there would be), it'd only get the players, you could also replace game.Workspace:WaitForChild(v.Name) with v.Character.

Code:

for i, v in pairs(game.Players:GetPlayers()) do
 v.Character:WaitForChild("Head").Transparency = 1
end

Please tell me if it work's, I wasn't able to test it.

Ad
Log in to vote
0
Answered by
drahsid5 250 Moderation Voter
8 years ago

Small mistake, you have to make it a table, that's an object.

for i, v in pairs(game.Players:GetChildren()) do
 game.Workspace:WaitForChild(v.Name):WaitForChild("Head").Transparency = 1
end

0
Did not work FiredDusk 1466 — 8y
0
What dies the output say? drahsid5 250 — 8y
0
No error FiredDusk 1466 — 8y
0
Try what I just posted drahsid5 250 — 8y
View all comments (6 more)
0
Still does not FiredDusk 1466 — 8y
0
Print v.name, tell me what it says. drahsid5 250 — 8y
0
stop with all the waitforchilds and then it will work HungryJaffer 1246 — 8y
0
Nonit wouldn't, if it's called whike a player is joining it will return an error. drahsid5 250 — 8y
0
Still does not :( FiredDusk 1466 — 8y
0
Tell me what it says when you add print(v.Name) drahsid5 250 — 8y
Log in to vote
0
Answered by 8 years ago

Alright, you forgot to call the method that will give you all the children inside Players. the most efficient method for that is :GetPlayers(). If you're using it in any other object, use :GetChildren(). The way you did would work for a table.

like:

Table = {"hi","bye"}

for i,stuff in pairs(Table) do  -- See, I didn't use GetChildren() or GetPlayers() here. It's a table.
print(i,stuff)
end

When you use :GetChildren() or GetPlayers(), you're basically turning the content into a table so you can use the loop.

for i,Player in pairs(game.Players:GetPlayers()) do
    if Player.Character then -- Checks if their character exists. It might have been removed for some reason or not spawned yet
        if Player.Character:FindFirstChild("Head") then
            Player.Character.Head.Transparency = 1
        end
    end
end

-- you can also tune it up and set some variables to save you from writing 'Player.Character.Head' over and over again

for i,Player in pairs(game.Players:GetPlayers()) do
    local Char = Player.Character
    if Char  then
        local Head = Char:FindFirstChild("Head")
        if Head  then
            Head.Transparency = 1
        end
    end
end
Log in to vote
0
Answered by 8 years ago
for _, v in pairs(game.Players:GetChildren()) do
    if v.Character ~= nil then
        v.Character.Head.Transparency = 1
    end
end

Answer this question