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

Why doesn't my loop change all parts' colors?

I have a simple script that is supposed to turn a player's skin tone to light blue.

hit.Parent["Body Colors"]:Destroy()
for i,v in pairs(hit.Parent:GetChildren()) do
	if v.ClassName == "Part" then
		v.BrickColor = BrickColor.new("Light blue")
	end
end

"hit" represents a limb, so hit.Parent is the character's model. For some reason it only does it to one part.

1 comment

1

if you add a wait() before the for loop it will work

2 answers

I am a bit rusty, so this may or may not work, but in my own experience I had to go do each individual body part when I wanted to change skin color/ material color/ transparency.

hit.Parent['Body Colors']:Destroy() -- Destroys Body Colors
	for _,v in pairs(hit.Parent:GetChildren()) do -- Turns v into all Children of Parent
		if v.ClassName == "Part" then -- Checks to see if the Childrens class is "Part"
			v.BrickColor = BrickColor.new("Light blue") -- Tries to turn all children with classname "part" to color Light blue
		end
	end

I am terrible at explaining what you did wrong, so let me see if I can come up with a solution. Remember I am rusty to high h*ll so forgive me.

hit.Parent['Body Colors':Destroy()
	for _,v in pairs(hit.Parent:GetChildren()) do
		if v.ClassName == "Part" then
			v["Left Leg"].BrickColor = BrickColor.new("Light blue")
			v["Right Leg"].BrickColor = BrickColor.new("Light blue")
			v["Left Arm"].BrickColor = BrickColor.new("Light blue")
			v["Right Arm"].BrickColor = BrickColor.new("Light blue")
			v["Torso"].BrickColor = BrickColor.new("Light blue")
			v["Head"].BrickColor = BrickColor.new("Light blue")
		end
	end
end	

This may or may not work, like I said I am pretty rusty.

2 comments

0

a "for loop" literates through a table, it doesnt just makes the variable everything in the table

0

also use :IsA() instead of checking the classname and use "BasePart" instead of "Part"

script.Parent.Touched:Connect(function(hit)
	hit.Parent:FindFirstChild("Body Colors"):Remove()
	for _,ctype in pairs(hit.Parent:GetChildren()) do -- ctype stands for class type
		if ctype:IsA("Part") then
		ctype.BrickColor = BrickColor.new("Light blue")
	end
	end
end)

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