Disappearing blocks managed in one script

I’m currently making a disappearing block system. It’s very simple, if you step on a block it will disappear, then reappear. The problem I’m having is that I don’t want an individual script in each block making it disappear.

The solution I found was to use the in pairs loop but the problem with this is that since there are a few wait() in the code, the table doesn’t loop until the first block that I stepped on disappears and reappears. But I need the blocks to continuously be disappearing as you step on them.

Here is the code I have so far:

local plateform = game.Workspace.DisapearingPlateforms:GetChildren()
local isTouched = false

for _, part in pairs(plateform) do
	
	if part:IsA("Part") then
		part.Touched:Connect(function(plr)
			if plr.Parent:FindFirstChild("Humanoid") then
				if not isTouched then
					isTouched = true
					for count = 1, 10 do
						part.Transparency = count / 10
						wait(0.1)
					end	
					part.CanCollide = false
					wait(3)
					part.Transparency = 0
					part.CanCollide = true
					isTouched = false
				end
			end
		end)
	end
end

I am a beginner scripter so if the solution is very simple, or if there is no solution at all, I’m very sorry for waisting your time. Any help is appreciated. :slight_smile:

1 Like

This is because of the scope of your isTouched variable.

local plateform = game.Workspace.DisapearingPlateforms:GetChildren()

for _, part in pairs(plateform) do
	if part:IsA("Part") then
        -- declare your isTouched variable inside of the loop
        local isTouched = false
-- continue with the rest of your script here

Basically what’s happening is that every single part sees the isTouched value is true when you first touch any part, then this causes it to return/exit the function each time it is touched until the first touched function sets the value to false.

3 Likes

you can use or add the

while true do
end

statement to your script so it does it every time – Probably won’t solve what you want.
Or you can check if a player is within range of the block instead of touching it.

1 Like