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

can I stop multiple functions at one time?

I have an ai script that shuts down immediately when a value changes, however in this function there are a couple waits, and if the value changes I have to wait for them to finish:

local speed = 10


function randomdirection()
	local bv = script.Parent.Parent.Parent.Parent.Center.BodyVelocity
	local bg = script.Parent.Parent.Parent.Parent.Center.BodyGyro
	local aimer = script.Parent.Parent.Parent.Parent.Center.BodyAim
	
	if script.Parent.Parent.IsAttacking.Value == true then
		bv.MaxForce = Vector3.new(0,40000,0)
		bv.Velocity = Vector3.new(0,0,0)
	end
	
	math.randomseed(tick()%1*1e6)
    local choose = math.random(1,4)

if choose == 1 and script.Parent.Parent.IsAttacking.Value == false then
	aimer.CFrame = CFrame.new(script.Parent.Parent.Parent.Parent.Center.Position,Vector3.new(25,0,0))
	wait(0.2)
	script.Parent.Forward.Value = true
	wait(2)
	script.Parent.Forward.Value = false
	return
	
	elseif choose == 2 and script.Parent.Parent.IsAttacking.Value == false then
	aimer.CFrame = CFrame.new(script.Parent.Parent.Parent.Parent.Center.Position,Vector3.new(-25,0,0))
	wait(0.2)
	script.Parent.Forward.Value = true
	wait(2)
	script.Parent.Forward.Value = false
	return
	
	elseif choose == 3 and script.Parent.Parent.IsAttacking.Value == false then
	aimer.CFrame = CFrame.new(script.Parent.Parent.Parent.Parent.Center.Position,Vector3.new(0,0,25))
	wait(0.2)
	script.Parent.Forward.Value = true
	wait(2)
	script.Parent.Forward.Value = false
	return
	
	elseif choose == 4 and script.Parent.Parent.IsAttacking.Value == false then
	aimer.CFrame = CFrame.new(script.Parent.Parent.Parent.Parent.Center.Position,Vector3.new(0,0,-25))
	wait(0.2)
	script.Parent.Forward.Value = true
	wait(2)
	script.Parent.Forward.Value = false
	return

end

end




while true do
	wait(5)
	randomdirection()
end

the waits after setting the cframe of "aimer" are what concern me, to avoid tedious checking, is there a way for me to shut down this function "from" the while true do loop?

3 comments

0

Do you want to stop the function from the while true do loop?

0

yes

0

use break on the loop

1 answer

✓ Accepted answer

You can do this in 2 ways, If you want to just break it after a wait() function for example wait(3) break and the other way is by using a bool and set it to false when you want to stop the while true do loop here is an example:

local examplebool = true -- Example bool

while true do -- While true do loop
if examplebool == true then -- If the bool is true then wait 5 seconds then do the function
wait(5)
randomdirection()
end
end

-- The rest of the script is only an example of how you could stop the while true do loop

script.Parent.Button123.MouseButton1Click:Connect(function()
examplebool = false
end)

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