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

How to make this loop forever until active is false and how to make a conveyor with :GetChildren()?

So I'm working on this right here;

local conveyors = script.Parent.conveyors:GetChildren()
repeat wait() until active.Value == true

	print('debug')
conveyors.Velocity = script.Parent.CFrame.lookVector *2
wait()

First thing; this code only runs once when active is true even though it's supposed to run forever until active is false, I just want this code to go through if active is true and stop the loop if active is false.

Another thing I want to make this conveyor section only work with one script instead of 1 in each part, how would I do that? I'm assuming it's GetChildren but whenever I run it I get CFrame is not a valid member of Model.

1 answer

✓ Accepted answer

To have the conveyors toggle when active is turned to true or false, use a while loop with a conditional (if) statement.

Next, you'll have to use :GetChildren() and loop over the contents, and apply velocity to each part.

local conveyors = script.Parent.conveyors:GetChildren()
local active = true


while true do
	if active then -- Will run the code inside if active is true.
		for i, conveyor in pairs(conveyors) do -- Loops through all conveyors and applies this effect to them.
			conveyor.Velocity = conveyor.CFrame.lookVector * 2
		end
	else -- This piece of code will run if active is false, noted by the else statement.
		for i, conveyor in pairs(conveyors) do 
			conveyor.Velocity = conveyor.CFrame.lookVector * 0 -- Stops the conveyors.
		end
	end
	wait() -- Make sure to have some sort of yield so we don't crash.
end

11 comments

0

How do I do conveyor.Velocity = conveyor.CFrame.lookVector *0 all in the same function, I'm a bit braindead at the moment.

0

Conveyors is a table containing all your your Conveyor parts. When you loop through it using the for loop I used, it will go through every single conveyor. You'll affect all of the conveyors with just a few lines of code.

0

I can't do if active.Value == false then, so what would be a placeholder for that?

0

What do you mean? The if statement won't run if active is false, and the conveyors will not be activated.

0

I know but I want to deactivate the conveyors when active is false or else it will run forever.

0

Yeah, that's the point. Each time the loop wait()s, it checks if active is true. If it is true, it will apply the force to each conveyor. If it is false, it does not apply the force, and effectively deactivates them.

0

You sure because it doesn't do the same for me.

0

Oh, really? Let me redo my answer if that isn't the case... I'll add an else statement.

0

Done again, check it out

0

oh duh, else

1

Thanks!

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