My npc doesn’t chase after player when it suppose to
Nothing inside the model is anchored. I though this might have been something to do with run service so I tried adding a check but it still doesn’t work
local RS = game:GetService("RunService")
local players = game:GetService("Players")
local Boss = script.Parent
local Humanoid = Boss.Humanoid
local BossRoot = Boss.HumanoidRootPart
local Animator = Humanoid.Animator
local AnimFolder = script.Parent.Animation
local char
local Action
local OldAction
local AttackStep = 0
local IsChasing = false
game.Players.PlayerAdded:Connect(function(plr)
char = plr.Character or plr.CharacterAdded:Wait()
end)
function Main()
Action = math.random(1,3)
if Action == OldAction then
Main()
return
end
if Action == 1 then
pcall(Stare)
elseif Action == 2 then
IsChasing = true
pcall(Chase)
elseif Action == 3 then
pcall(Range)
end
OldAction = Action
RS.Stepped:Connect(function()
if char then
BossRoot.CFrame = CFrame.new(BossRoot.Position,Vector3.new(char.Torso.Position.X,BossRoot.Position.Y,char.Torso.Position.Z))
end
end)
end
function Chase()
print("Current Action: Chasing")
RS.Stepped:Connect(function()
if IsChasing and char then
local distance = (char.PrimaryPart.Position - BossRoot.Position).Magnitude
if distance >= 10 then
Humanoid:MoveTo(char.PrimaryPart.Position, char.PrimaryPart)
else
Melee()
end
end
end)
end
function Stare()
print("Current Action: Staring")
task.delay(3, Main)
end
function Range()
print("Current Action: Using Range(Not yet finished)")
Main()
end
function Melee()
print("Current Action: Melee Attack")
IsChasing = false
for i = 1, 4, 1 do
AttackStep = i
local CurrentAnim = AnimFolder:FindFirstChild("Slash (" ..AttackStep.. ")")
local Track = Animator:LoadAnimation(CurrentAnim)
Humanoid:MoveTo(BossRoot.CFrame.LookVector*2, BossRoot)
Track:Play()
task.wait(Track.Length - 0.1)
end
task.wait()
Main()
end
Main()