So I have a while loop that causes an NPC to MoveTo a player while they’re in attack mode.
However, when they’re out of attack mode they still move towards the player.
I’ve tried doing :Move(Vector3.new(0,0,0)) when the mode changes, but that doesn’t cancel it. I’ve also tried usig MoveTo() again to make it move to it’s own posistion. But it still only moves towards the player it was attacking.
script.Parent.AttributeChanged:Connect(function(attribute)
local list = {}
for i,v in pairs(script.Parent.Parent:GetChildren()) do
if v:IsA("Model") then
table.insert(list,#list + 1,v)
end
end
local stage = script.Parent:GetAttribute("Stage")
if stage == 'Attacker' or stage == 'Attacker' then
local is_attacking = stage == 'Attacker' or stage == 'Attacker'
local targ = script.Parent.Target.Value
local hrp = script.Parent.HumanoidRootPart
while script.Parent.Target.Value ~= nil and is_attacking do
wait()
if stage == 'Attacker' or stage == 'Attacker' then
local cooldownwait = math.random(atcdmin,atccmax)
if (hrp.Position - targ.Position).magnitude >= 4 then
local dest = (script.Parent.Target.Value.Position - (script.Parent.Head.CFrame.LookVector * 4))
local path = pathfind:CreatePath()
path:ComputeAsync(script.Parent.HumanoidRootPart.Position,dest)
if stage == 'Attacker' or stage == 'QuickAttacker' then
hum:MoveTo(dest)
end
end
if (hrp.Position - targ.Position).magnitude <= 7 then
hum:Move(Vector3.new(0,0,0))
if cooldown == false then
hum:Move(Vector3.new(0,0,0))
if script.Parent:GetAttribute('Stage') == 'Attacker' then
mod.Attack(char,anims,13,.75,char.Tool.Blade,.65,false)
elseif script.Parent:GetAttribute('Stage') == 'QuickAttacker' then
mod.Attack(char,anims,13,.75,char.Tool.Blade,.65,true)
end
cooldown = true
delay(cooldownwait, function() cooldown = false end)
hum:Move(Vector3.new(0,0,0))
if script.Parent:GetAttribute('Stage') == 'Attacker' then
local maxattackers = script.Parent.Parent.Parent:GetAttribute('MaxAttackers')
if callcd == false and (mod.CountStages('Stage','QuickAttacker',list) + mod.CountStages('Stage','Standby',list)) < (maxattackers) then
mod.CallForHelpAttack(script.Parent,1,list,script.Parent.Target.Value)
callcd = true
delay(10,function() callcd = false end)
end
end
if stage == 'QuickAttacker' then
if math.random(1,3) == 1 then
local attackers = mod.GetNpcsOfStage('Attacker',list)
for i,v in pairs(attackers) do
--v:SetAttribute('Stage','QuickAttacker')
end
--script.Parent:SetAttribute('Stage','Attacker')
end
end
end
end
end
end
The next part is the attack function that chances the attribute after.
local function Swing()
if char:GetAttribute('Stage','Attacker') or char:GetAttribute('Stage','QuickAttacker') then
char:SetAttribute('Stage','Swinging')
char.Head.AttackIndicator.Enabled = false
local hitbox = rayhitbox:Initialize(blade,{char,char.Parent:GetChildren()})
hitbox:DebugMode(false)
local function MoveForward()
hum:Move(Vector3.new(0,0,0))
hum.WalkSpeed = 3
local movetopos = Vector3.new(0,0,-1)
hum:Move(movetopos)
delay(.3,function() loadanim:Play() end)
delay(.65,function()hum:Move(Vector3.new(0,0,0))end)
hum.WalkSpeed = speed
if quick == true then
char:SetAttribute('Stage','Triggered')
elseif quick == false then
char:SetAttribute('Stage','Attacker')
end
end
coroutine.resume(coroutine.create(MoveForward))
hitbox.OnHit:Connect(function(hit,humanoid)
humanoid:TakeDamage(dmg)
end)
hitbox:HitStart()
delay(swingtime,function() hitbox:HitStop() end)
end
end
This is the part that runs when the attribute is changed to triggered
elseif stage == 'Triggered' then
local anim = script.Parent.Anims.Idle
local loadanim = hum:LoadAnimation(anim)
loadanim.Looped = true
loadanim:Play()
script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
while stage == 'Triggered' do
wait(1)
local hrp = script.Parent.HumanoidRootPart
local targ = script.Parent.Target.Value
if script.Parent.Target.Value ~= nil and (hrp.Position - targ.Position).magnitude >= npcdistance then
script.Parent.Humanoid:MoveTo((targ.Position + Vector3.new(math.random((-1 * npcdistance),npcdistance),0,math.random((-1 * npcdistance),npcdistance)) - (script.Parent.Head.CFrame.LookVector * 5)))
end
if script.Parent.Target.Value == nil then
script.Parent:SetAttribute('Stage','Idle')
end
local list = {}
end
end
end)
Any ideas on how I can cancel the movement?