Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why are my blocks spazzing out? (Output isn't giving any errors) Video included

My floor is supposed to change from outter layer to last layer in color, like in those club's like club sanity. Here's my script:

local outter = game.Workspace.outter:GetChildren()
local second = game.Workspace.second:GetChildren()
local third = game.Workspace.third:GetChildren()
local last = game.Workspace.last:GetChildren()

while true do
	for i,v in pairs(outter) do
		spawn(function()
		v.BrickColor = BrickColor.Red()
		wait(1)
		v.BrickColor = BrickColor.Black()
		end)
	end
	for b,n in pairs(second) do
        spawn(function()
		n.BrickColor = BrickColor.Red()
		wait(1)
		n.BrickColor = BrickColor.Black()
	    end)
	end
	for h,j in pairs(third) do
        spawn(function()
		j.BrickColor = BrickColor.Red()
		wait(1)
		j.BrickColor = BrickColor.Black()
	    end)
	end
	for o,p in pairs(last) do
		spawn(function()
		p.BrickColor = BrickColor.Red()
		wait(1)
		p.BrickColor = BrickColor.Black()
	        end)
		end
	wait(1)
	
end

Here's a video of what is going on: https://gyazo.com/43c8a28164465867f24b264b2a2b8245

All help is appreciated!

1 answer

✓ Accepted answer

The problem with this code is that it will loop through each group and spawn a function to process a block then it will wait. To do this task you would need to change the colours then wait and move onto the next group.

Example:-

while true do
	-- we create a thread for each block
    for h,j in pairs(third) do
        spawn(function()
        j.BrickColor = BrickColor.Red()
        wait(1)
        j.BrickColor = BrickColor.Black()
        end)
    end
	-- we do not wait so there will be no changes
	-- we also add the next group
    for o,p in pairs(last) do
        spawn(function()
        p.BrickColor = BrickColor.Red()
        wait(1)
        p.BrickColor = BrickColor.Black()
            end)
        end
    wait(1) -- we then wait at the end
end

You can also create function to handle each step so as to not duplicate code. I am also going to use a nested array for convenience:-

-- the nested array of the parts
local data = {
	game.Workspace.outter:GetChildren(),
	game.Workspace.second:GetChildren(),
	game.Workspace.third:GetChildren(),
	game.Workspace.last:GetChildren()
}

-- number of lists and the current processing list
local numOfLists = #data
local listNum = 1

-- a function to set one list of blocks to the brick colour given
local function changeBrickColor(list, newBrickColor)
	for _, block in pairs(last) do
		block.BrickColor = newBrickColor
	end
end

-- processing for a group of blocks, this is the colour change sequence 
local function processBlock(block)
	changeBrickColor(block, BrickColor.Red())
	wait(1)
	changeBrickColor(block, BrickColor.Black())
end

while true do
	wait(1)
	processBlock(data[listNum])
	listNum = listNum + 1
	if listNum > numOfLists then listNum = 1 end
end

I hope this helps, please comment if you do not understand how / why this code works.

1 comment

0

I have no idea how the second part of the code works, but I understand the first part. That part helped. Thanks! You don't need to explain the second part. I'd rather go step by step slowly. I'll get into the stuff you're showing later on. Thanks so much for your help! :D

This discussion is preserved as it was. New answers, comments, and votes are closed.