I made an NPC that chases a player. I made the sctipt but the NPC move direction stays at 0.
The script needs the move direction to work. Are there any other ways to replace MoveDirection?
NPCs do not use MoveDirection, its a thing used to figure out the desired direction a client wishes to move a humanoid.
what would I use to replace it?
If you could show what you need it for, we could probably see what other things you can use.
Makes NPC snap turn
local character = script.Parent
local Humanoid = character.Humanoid
local HRP = character:WaitForChild("HumanoidRootPart")
Humanoid:GetPropertyChangedSignal('MoveDirection'):Connect(function()
if Humanoid.MoveDirection.X > .1 then
HRP.CFrame = CFrame.new(HRP.CFrame.Position, Vector3.new(HRP.CFrame.Position.X + 2, HRP.CFrame.Position.Y, HRP.CFrame.Position.Z))
elseif Humanoid.MoveDirection.X < -.1 then
HRP.CFrame = CFrame.new(HRP.CFrame.Position, Vector3.new(HRP.CFrame.Position.X - 2, HRP.CFrame.Position.Y, HRP.CFrame.Position.Z))
end
end)
what i did because i encountered a similar issue, is i just stored a value in the NPC script of where i wanted it to move to, and then i just used that instead of the movedirection.
How would I do that?(Sorry I’m newer with coding)
so normally to move an NPC you’d use the functions
Humanoid:MoveTo(--[[position here]])
-- or
Humanoid:Move(--[[direction]])
i’d create two functions called Move and MoveTo and just make it so that when its called it stores a variable called MoveDir based on the information i’ve passed through the function.
MoveDir = Vector3.zero
rootPart = script.Parent.HumanoidRootPart
humanoid = script.Parent.Humanoid
function MoveTo(position)
MoveDir = (position - rootPart.Position).Unit
humanoid:MoveTo(position)
end
function Move(direction)
MoveDir = direction
humanoid:Move(direction)
end
This is the chase player script. Idk if I did your way right. I don’t see any errors on your script
local NPC = workspace.Enemy
NPC.PrimaryPart:SetNetworkOwner(nil)
local pfs = game:GetService("PathfindingService") -- Pathfinding service
local runService = game:GetService("RunService")
local hum = script.Parent:WaitForChild("Humanoid") -- npc humanoid
local torso = script.Parent:WaitForChild("HumanoidRootPart") -- npc torso
local players = game:GetService("Players")
local distance
local closestPlayer, closestDistance = nil, math.huge
function checkPlayerDistance() -- Function will loop through the players and get the nearest player
local children = players:GetChildren()
for i,v in pairs(children) do
if v then
distance = (v.Character:FindFirstChild("HumanoidRootPart").Position - torso.Position).magnitude
if distance <= closestDistance then
closestDistance = distance
closestPlayer = v
end
end
end
return closestPlayer
end
runService.Heartbeat:Connect(function() -- NPC will follow the nearest player
local returnedPlayer = checkPlayerDistance()
local path = pfs:CreatePath()
path:ComputeAsync(torso.Position, returnedPlayer.Character.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
local function npcStuck()
local pos1 = torso.Position
wait(1)
local pos2 = torso.Position
if (pos1 - pos2).magnitude < 1 then
end
end
if waypoints and waypoints[2] then
local pos = waypoints[2]
hum:MoveTo(pos.Position)
end
closestDistance = math.huge
end)
so you see how you do hum:MoveTo(pos.Position)? what i did for my script was instead of using the function of the humanoid right away, i made two functions called the same things and stored the move direction based on the vector i parsed through the function. then in the function i called hum:MoveTo().