NPC doesn't move with MoveTo()

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()

Try doing player.characteradded for char variable like do characteradded(c) then char = c, do it inside playeradded

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 OldAction
local AttackStep = 0
local IsChasing = false

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		char = character
	end)
end)

function Main()
	local newAction = math.random(1,3)
	if newAction == OldAction then
		Main()
		return
	end
	if newAction == 1 then
		IsChasing = false
		pcall(Stare)
	elseif newAction == 2 then
		IsChasing = true
		pcall(Chase)
	elseif newAction == 3 then
		IsChasing = false
		pcall(Range)
	end
	OldAction = newAction
end


function Chase()
	print("Current Action: Chasing")
	while true do
		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
		task.wait(1)
	end
end

function Stare()
	print("Current Action: Staring")
	task.delay(3, Main)
end

function Range()
	print("Current Action: Using Range(Not yet finished)")
	task.delay(3, 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)
		Track:Play()
		task.wait(Track.Length - 0.1)
	end
	task.wait()
	Main()
end

Main()

ok I kinda accidentally solve my problem
Turn out because of the runservice, the script stuck in the staring loop and can’t get to run the moveTo loop which is why it isn’t working

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