Unable to change models transparency in a for loop

I’m trying to make it so if a BoolValue is set to true, all of the children of a models’ transparency is set to 0 and CanCollide is set to true, for some reason this is not working, the BoolValue is being set to true and it’s throwing no error, but CanCollide and Transparency are not being changed.

Script inside of model:

script.Parent.Parent.SecondActivated:GetPropertyChangedSignal("Value"):Connect(function()

if script.Parent.Parent.SecondActivated.Value == true then

for i, v in pairs(script.Parent:GetChildren()) do

if v:IsA("Part") then

v.Transparency = 0

v.CanCollide = true

end

end

elseif script.Parent.Parent.SecondActivated.Value == false then

for i, v in pairs(script.Parent:GetChildren()) do

if v:IsA("Part") then

v.Transparency = 1

v.CanCollide = false

end

end

end

end)```

When using :GetChildren() you have to use ipairs not pairs for example the line:

for i, v in pairs(script.Parent:GetChildren()) do

has got to be changed to

for i, v in ipairs(script.Parent:GetChildren()) do

This is the same for the other line using GetChildren.

1 Like
while wait(4) do
	if script.Parent.Parent.SecondActivated.Value == true then
		local pumpkinClone = pumpkin:Clone()
		pumpkinClone.Parent = workspace
		pumpkinClone.CFrame = CFrame.new(script.Parent.DropperPart.Position)
	end
end

script.Parent.Parent.SecondActivated:GetPropertyChangedSignal("Value"):Connect(function()
	if script.Parent.Parent.SecondActivated.Value == true then
		for i, v in ipairs(script.Parent:GetChildren()) do
			if v:IsA("Part") then
				v.Transparency = 0
				v.CanCollide = true
			end
		end
	elseif script.Parent.Parent.SecondActivated.Value == false then
		for i, v in ipairs(script.Parent:GetChildren()) do
			if v:IsA("Part") then
				v.Transparency = 1
				v.CanCollide = false
			end
		end
	end
end)```

For some reason this still doesn't work.

It is working fine for me which is the part that is not working?

Try using v:IsA(“BasePart”) instead. I’m pretty sure meshparts won’t be accounted for if you use Part

You don’t have to use ipairs for iterating through the table returned by GetChildren

The while loop yields the script, move it to the bottom:

script.Parent.Parent.SecondActivated:GetPropertyChangedSignal("Value"):Connect(function()
	if script.Parent.Parent.SecondActivated.Value == true then
		for i, v in ipairs(script.Parent:GetChildren()) do
			if v:IsA("Part") then
				v.Transparency = 0
				v.CanCollide = true
			end
		end
	elseif script.Parent.Parent.SecondActivated.Value == false then
		for i, v in ipairs(script.Parent:GetChildren()) do
			if v:IsA("Part") then
				v.Transparency = 1
				v.CanCollide = false
			end
		end
	end
end)

while wait(4) do
	if script.Parent.Parent.SecondActivated.Value == true then
		local pumpkinClone = pumpkin:Clone()
		pumpkinClone.Parent = workspace
		pumpkinClone.CFrame = CFrame.new(script.Parent.DropperPart.Position)
	end
end
1 Like

Maybe use GetDescantants() instead? Or there are only children?

Oh yeah I just tested it I am not sure why it wasnt working before then