NPC scripting issue

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

4 Likes

What exactly do you mean by checking for player? checking if they are in range?

updating the players position so when the player moves the NPC knows that and keeps moving towards the player and not where the players last location was.

I believe youre going to have to loop :MoveTo()

and then this could be a separate function then? i would have to still be able to stop when i call an attack function and start again once the attack function is finished

looping :moveTo() doesnt update the players position fast enough which is a problem since this is an enemy u dont run away from so its just gonna walk past the player if they move toward the NPC

Man im sorry but i dont quite understand where were going at here :skull:

so the way i understand this is you wanna make a function that moves the Boss to the player correct?

im trying to make sure the NPC knows where the player is at all times whether if its moving to the player or not (edited because i realised what i said only made sense in my head)
the only thing i tried that works is using runservice but i have to be able to turn it off when it isnt needed then turn it back on when it is needed

For anyone who comes across this post still, the answer was in my script all along. setting debounce to true then calling the function works perfectly so now i have achieved what i wanted one function is now two

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.