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
Do you want to stop the function from the while true do loop?
yes
use
breakon the loop