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

How to change Model's material with a script?

My goal is to make blinking indicators that change material with a short intermission at the same time. So far I opted using the GetChildren() method, however, while it does change the material once, it doesn't return to the former material as stated in the script. If I use the in pairs loop, the parts don't change material at the same time, but instead they change material individually!

I have tried many solutions already, can you show me the ropes? Script:

local LightPart = script.Parent:GetChildren()

while true do
	LightPart.Material = "Neon"
	wait(1)
	LightPart.Material = "SmoothPlastic"
end	

2 comments

0

Im pretty sure you have to use the following code: LightPart.Material = Material.new("Neon"), I don't know for sure though because I've never wanted to change a part's material.

0

Material.new?? O.O yeah no

1 answer

The correct way to do it would be to have a for loop to get all the children from the table instead of the table itself, and also check if its a base part so you don't try to set the material of scripts too.

local LightPart = script.Parent:GetChildren() -- Getting the Children.

while true do -- Starting the while loop.
	for i,v in pairs(LightPart) do -- Getting all parts from the table.
		if v:IsA("BasePart") then -- Checking if it is actually a part.
			v.Material = "Neon" -- Setting the material to neon.
			wait() -- to reduce lag, although it is not needed.
		end -- Ending the if statement.
	end -- Ending the for loop.
	wait(1) -- Waiting.
	for i,v in pairs(LightPart) do -- Getting all parts from the table again.
		if v:IsA("BasePart") then -- Making sure that it's actually a part again.
			v.Material = "Plastic" -- Setting the material back to plastic.
			wait() -- to reduce lag, although it is not needed.
		end -- Ending the if statement.
	end -- Ending the for loop again.
end -- Ending the while loop.

you tried to change the material of the table itself and didn't even check if it's a basepart, to get objects from a table you have to have a for loop.

local LightPart = script.Parent:GetChildren() -- Getting the Children.

while true do -- Starting the while loop.
	LightPart.Material = "Neon"
	wait(1)
	v.Material = "Plastic"
end

Make sure to accept if this helped!

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