How do I make multiple parts with the name go invisible and transparent with a script?

So i’ve got a script thats supposed to make several parts go invisible and transparent but it only makes 2 of them do it

this is the script:

collide = true

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
	for i, v in pairs(game.workspace:GetChildren()) do
		if v.Name == "Swap" then
				if collide == true then
					v.Transparency = 1
					v.CanCollide = false
					collide = false
				else
					v.CanCollide = true
					v.Transparency = 0
					collide = true
			end
		end
	end
end)

Any help would be greatly appreciated and im willing to answer any questions!

1 Like

Do this instead:

game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent").OnServerEvent:Connect(function()
	for i, v in pairs(game.workspace:GetChildren()) do
		if v.Name == "Swap" then
				if v.CanCollide then
					v.Transparency = 1
					v.CanCollide = false
					collide = false
				else
					v.CanCollide = true
					v.Transparency = 0
					collide = true
			end
		end
	end
end)

You put your collide variable outside the function, causing it to be set to true/false each iteration. You want to reference the CanCollide value instead.

1 Like

Ohhh, yeah i see now. thank you!
i also ended up removing the “collide = false” and “collide = true” because those arent needed

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.