I don’t know where to start, but I’m trying to create a NPC that follows the closest player without using Path Finding Service. This is my current script
local ClosestMagnitude = math.huge
local ClosestPlayer = nil
local Part = game.Workspace.Model.HumanoidRootPart
local function JumpOverObstacles()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Part.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local Body,Head,AboveHead = workspace:Raycast(Part.Position, Part.CFrame.LookVector, raycastParams),workspace:Raycast(Part.Parent.Head.Position + Vector3.new(0,3,0), Part.Parent.Head.CFrame.LookVector*5, raycastParams)
if Body or Head then
Part.Parent.Humanoid.Jump = true
end
end
local function JumpOverSpaces()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Part.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(Part.Position, Part.CFrame.LookVector*3 - Vector3.new(0,3,0), raycastParams)
if not raycastResult then
Part.Parent.Humanoid.Jump = true
end
end
while wait() do
ClosestMagnitude = math.huge
for _, v in pairs(game.Players:GetPlayers()) do
if v.Character then
local Magnitude = (Part.Position - v.Character.HumanoidRootPart.Position).Magnitude
if Magnitude < ClosestMagnitude then
ClosestMagnitude = Magnitude
ClosestPlayer = v
end
end
end
if ClosestPlayer ~= nil and ClosestMagnitude <= 20 then
JumpOverObstacles()
JumpOverSpaces()
Part.Parent.Humanoid:MoveTo(ClosestPlayer.Character.HumanoidRootPart.Position)
end
end
``` This works, but sometimes it gets stuck under a part due to it being mid jump. Anyone have any advice on where I can start fixing this issue?