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

Help with i,v in pairs?

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

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.

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

10 comments

0

Did not work

0

What dies the output say?

0

No error

0

Try what I just posted

0

Still does not

0

Print v.name, tell me what it says.

0

stop with all the waitforchilds and then it will work

0

Nonit wouldn't, if it's called whike a player is joining it will return an error.

0

Still does not :(

0

Tell me what it says when you add print(v.Name)

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
for _, v in pairs(game.Players:GetChildren()) do
	if v.Character ~= nil then
		v.Character.Head.Transparency = 1
	end
end

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