I am creating an NPC for a boss fight. The goal of the NPC is to target the player, move to them then attack.
So my script works after messing around with it for a while , being new to coding i managed to work everything into one function. The problem with this is to complete it this way i think i would have to do it all in this one function which is not my goal
im using heartbeat so it keeps checking for the player within the function which is where i think the issue is but when i remove heartbeat and use .moveToFinished() it no longer keeps checking for the player
my end goal for the script is for multiple attacks so i would need the checking for player to be a seperate thing (i think)
you can see here what ive done so far
--
local NPC = script.Parent
local NPCHum = script.Parent:FindFirstChildOfClass("Humanoid")
local NPCHumRoot = script.Parent.NPCHumanoidRootPart
NPCHum.WalkSpeed = 10
NPCHum.MaxHealth = 1000
NPCHum.Health = 1000
local Plr = game.Players.PlayerAdded:Wait()
local Char = Plr.Character
if not Char or not Char.Parent then
Char = Plr.CharacterAdded:Wait()
end
local PlrHum = Char:FindFirstChildOfClass("Humanoid")
local PlrHumRoot = Char:WaitForChild("HumanoidRootPart")
local runService = game:GetService("RunService")
local anim = script["Boss Strike"]
local walk = script["WalkAnim"]
local animator = NPCHum.Animator
local track = animator:LoadAnimation(anim)
local walkTrack = animator:LoadAnimation(walk)
ATKDistance = 10
AggroDistance = 100
Damage = 20
local debounce = false
local function attack()
if debounce then
return
end
if PlrHumRoot then --moves to player
NPCHum:MoveTo(PlrHumRoot.Position)
debounce = true
walkTrack:Play()
task.wait(0.5)
debounce = false
end
if (NPCHumRoot.Position - PlrHumRoot.Position).Magnitude < ATKDistance then
debounce = true
NPCHum.WalkSpeed = 0 -- stops in front of the player
NPCHumRoot.Anchored = true
walkTrack:Stop()
task.wait(0.1)
track:Play() --attacks
wait(2)
NPCHumRoot.Anchored = false
NPCHum.WalkSpeed = 10 --resumes moving to the player
debounce = false
end
end
runService.Heartbeat:Connect(attack)
im just not sure how to turn this around into what im trying to achieve. I’m trying to explain the best i can but i might be able to explain better through questions if i’ve left it confusing
To be clear im only needing help with making the NPC always check for the player without being looped in this way, although my script works its not the right way to do it for what im wanting. i think its either the checking forplayer has to be in separate function or checking and moving toplayer should be