Is this the right way to change the transparency of a model?

Im making a loop that waits and changes the transparency of a model but is this the right way to do it?

local one = game.Workspace.model1:GetChildren()
local two = game.Workspace.model2:GetChildren()

    while true do
    	wait(1)
    	for i,g in pairs(one) do
    		g.Transparency = 0
    	end
    	wait(1)
    	for i,v in pairs(two) do
    		v.Transparency = 0
    	end
    end
  1. Is there a reason that you have it in a while loop? Unless you’re changing it from somewhere else, you’ll just be constantly resetting it to 0 for no reason.
  2. You don’t need a wait() in between the two for loops, just one is enough.
  3. You should be using ipairs instead of pairs, since :GetChildren() returns an array.
  4. Make sure the child is a BasePart using the :IsA() function before you try to set transparency, because only BaseParts have a transparency property.

Well there is a reason for the loop, the loop changes the transparency of the model and the other model and the other model, im working on it but i need to see if im doing in the right way

I mean the while loop. It’s going to repeat and do the same thing multiple times when it only needs to do it once.

I dont think you are understanding, the loop is gonna change the transparency of the first model to 1 and the other to 0 then the first to 0 then the other to 1

Your current code just sets it to 0. I think you are looking for something like this:

local one = workspace.model1:GetChildren()
local two = workspace.model2:GetChildren()

while wait(1) do
    for _, instance in ipairs(one) do
        if instance:IsA("BasePart") then
            instance.Transparency = instance.Transparency == 0 and 1 or 0
        end
    end

    wait(1)
    for _, instance in ipairs(two) do
        if instance:IsA("BasePart") then
            instance.Transparency = instance.Transparency == 0 and 1 or 0
        end
    end
end

I don’t understand why you do while true do.

it works but both models stay visible and after a while they all dissapear and appear

What is the effect that you are actually trying to achieve?

The effect that the stickbug moves, i have two models that are different poses, and im trying to make a loop that changes the transparency of each of them so they look that is moving

Try this:

local one = workspace.model1:GetChildren()
local two = workspace.model2:GetChildren()

local currentFirst = 0

while wait(1) do
    for _, instance in ipairs(one) do
        if instance:IsA("BasePart") then
            instance.Transparency = currentFirst
        end
    end

    local flipped = currentFirst == 0 and 1 or 0
    currentFirst = flipped

    wait(1)
    for _, instance in ipairs(two) do
        if instance:IsA("BasePart") then
            instance.Transparency = flipped
        end
    end
end

Still looks the same as before

Do you want both of them to be invisible at the same time?

No (30 charsssss)

So you want them to alternate

local one = workspace.model1
local two = workspace.model2

local parts = {} -- put all our stuff in one table so we only need to go through one loop per cycle.
for _, part in ipairs(one:GetChildren()) do
	if (part:IsA("BasePart")) then
		table.insert(parts, part)
	end
end
for _, part in ipairs(two:GetChildren()) do
	if (part:IsA("BasePart")) then
		table.insert(parts, part)
	end
end

local step = true
while (true) do -- don't use "while wait(1) do", there's a post about it somewhere
	for _, part in ipairs(parts) do
		if (part.Parent == one) then
			part.Transparency = step and 0 or 1
		else
			part.Transparency = step and 1 or 0
		end
	end
	step = not step -- alternate
	wait(1)
end
1 Like

Yes, thank you that was what I was looking for!