How can I make 1 part appear and disappear, so another part appears?

Hello Developers.

I’m currently trying to make a new game, I’m familiar with Lua and I can understand the language, but sadly I cannot script anything yet. I’ve tried searching for the script I need on social media but still haven’t found it, that’s why I’m asking you.
It is a short script so it should be fine.

I’m searching for a for a script that makes 2 parts work like this: A blue block stays visible for 3 seconds, you also can jump and stand on it, the usual. But after those 3 seconds, the blue part disappears and a red part appears for 3 seconds. You can stand on it again, and after the time’s up, it changes to the blue part again, a loop.

2 Likes

We’ll use BasePart.Transparency, BasePart.CanCollide, along with an infinite loop. We can use wait to yield for a certain amount of seconds:

local function setVisible(part, isVisible)
    part.Transparency = isVisible and 0 or 1
    part.CanCollide = isVisible
end

while true do
    setVisible(part2, false)
    setVisible(part1, true)
    wait(3)
    setVisible(part1, false)
    setVisible(part2, true)
    wait(3)
end
2 Likes

Try this.

local Part1 = game.Workspace:WaitForChild("Part1")
local Part2 = game.Workspace:WaitForChild("Part2")

while true do
	Part1.Transparency = 0
	Part1.CanCollide = true
	Part2.Transparency = 1
	Part2.CanCollide = false
	wait(3)
	Part1.Transparency = 1
	Part1.CanCollide = false
	Part2.Transparency = 0
	Part2.CanCollide = false
	wait(3)
end

I made a .rbxm file if you need.
Part.rbxm (2.8 KB)

Part1 is Blue, Part2 is Red.

2 Likes

Transparency and CanCollide, if you want multiple parts to be set visible

local RedPartFolder = workspace["RedPartFolder"]
local BluePartFolder = workspace["BluePartFolder"]

local Toggle = false

local function setVisible(part, isVisible)
    part.Transparency = isVisible and 0 or 1
    part.CanCollide = isVisible
end

while wait(3) do
    if Toggle then
        Toggle = false
    else
        Toggle = true
    end

    for i, part in pairs(RedPartFolder:GetChildren()) do
        setVisible(part, Toggle)
    end

     for i, part in pairs(BluePartFolder:GetChildren()) do
        setVisible(part, not Toggle)
    end
end

@Dandystan Small error: There is only 1 yield.

1 Like

By the way, how do you add multiple parts to be in that loop, because I want multiple parts do the same thing. ))

You forgot to set Part2’s CanCollide to true when you make it visible.

Yeah noticed that, I fixed it.

@Dandystan Oh yeah. Sorry, my bad.
@Crashstyler747 Here is a Script to do it:

local RedParts = {}
local BlueParts = {}
local Showed = "Red"

for _, v in pairs(game.Workspace.Parts:GetChildren()) do
	if v.Name == "Red" then
		table.insert(RedParts,v)
	elseif
		v.Name == "Blue" then
			table.insert(BlueParts,v)
	end
end

function ToggleParts()
	if Showed == "Red" then
		for _, v in pairs(RedParts) do
			v.Transparency = 1
			v.CanCollide = false
		end
		for _, v in pairs(BlueParts) do
			v.Transparency = 0
			v.CanCollide = true
		end
		Showed = "Blue"
	elseif
		Showed == "Blue" then
			for _,v in pairs(RedParts) do
				v.Transparency = 0
				v.CanCollide = true
			end
			for _, v in pairs(BlueParts) do
				v.Transparency = 1
				v.CanCollide = false
			end
			Showed = "Red"
	end
end

while true do
	wait(0.5)
	ToggleParts()
	wait(0.5)
end

Showed is a variable that tells us which Parts are currently showed. Everytime you run the Function ToggleParts() the Red parts will disappear and there will appear the Blue parts.
Insert into a Folder, in Workspace, called “Parts” as many Parts as you want. Just make sure that the Red Parts are called “Red” and the Blue ones are called “Blue”.

MultipleParts.rbxm (3.6 KB)

Or you could use my solution by using folders. Parent the parts to their respective color folder, RedPartFolder and BluePartFolder are folders containing parts.

To be honest, using folders is better than naming each part if you’re lazy.

Or, you could use CollectionService to tag blue parts and red parts separately and use CollectionService:GetTagged to make all parts of a certain collection (in)visible.

1 Like

You’re right. But you can rename multiple parts at once by selecting some parts (CTRL+CLICK) and changing the name in properties.

Scripting Support is not a venue to ask for code. Please do not make such threads in the future. See the category guidelines for information on the category.

1 Like

I mean, true. Since I’m a new member, thank you for the feedback message.