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

Why is it changing only one texture instead of all of them?

A part has 6 textures on every side, and this part has the script below. I want the script to change all the texture components at the same time every 1 second, but it only changes 1 texture from one at a time. Why is it changing only one texture instead of all of them? I changed the ID of the decal so you others don't use it.

while true do
	for _, child in pairs(script.Parent:GetChildren()) do
		if child.ClassName == "Texture" then
			child.Texture = "rbxassetid://(ID)"
			wait(1)
			child.Texture = "rbxassetid://(ID)"
			wait(1)
		end
	end
end

2 answers

✓ Accepted answer

Your issue here is where you're waiting:

while true do
    --Modify all textures
    for _, child in pairs(script.Parent:GetChildren()) do
        if child:IsA("Texture") then
            child.Texture = "rbxassetid://(ID)"
        end
    end

    --THEN wait
    wait(1)

    -THEN modify all textures
    for _, child in pairs(script.Parent:GetChildren()) do
        if child:IsA("Texture") then
            child.Texture = "rbxassetid://(ID)"
        end
    end

    --THEN wait
    wait(1)
end

You have to be extremely careful where you use wait(). Your original code is looping through every texture, but because you're waiting inside that loop, you're changing one side, waiting, changing it back, waiting, then moving on to the next side instantly.

1 comment

0

Thanks!

This will change one part at a time because you are waiting inside the loop, change the textures then wait and repeat. It would also be good to use a function to stop the duplication of code.

Example:-

local function changeTexture(id, parts)
	id = 'rbxassetid://' .. tostring(id)
	for _, part in pairs(parts) do
		if part.ClassName == 'Texture' then part.Texture = id end
	end
end

while true do
	changeTexture([some id],script.Parent:GetChildren())
	wait(1)
	changeTexture([some id],script.Parent:GetChildren())
	wait(1)
end

If the parent parts do not change then it would be best to make a table of Texture objects and reuse it each time you want to change the texture of the parts.

I hope this helps, please comment if you do not understand how / why this code works.

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