so im trying to make this enemy npc be able to jump when an obstacle/terrain is infront of them
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local humanoid = script.Parent
local root = humanoid.Parent.PrimaryPart
local targetDistance = 30
local stopDistance = 0
function findNearestPlayer()
local playerList = Players:GetPlayers()
local nearestPlayer = nil
local distance = nil
local direction = nil
for _, player in pairs(playerList) do
local character = player.Character
if character then
local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)
if not nearestPlayer then
nearestPlayer = player
distance = distanceVector.Magnitude
direction = distanceVector.Unit
elseif distanceVector.Magnitude < distance then
nearestPlayer = player
distance = distanceVector.Magnitude
direction = distanceVector.Unit
end
end
end
return nearestPlayer, distance, direction
end
RunService.Heartbeat:Connect(function()
local nearestPlayer, distance, direction = findNearestPlayer()
if nearestPlayer then
if distance <= targetDistance and distance >= stopDistance then
humanoid:Move(direction)
else
humanoid:Move(Vector3.new())
end
end
end)
You don’t seem to be using PathfindingService, which would allow the npc to jump:
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TARGET_DISTANCE = 30
local STOP_DISTANCE = 0
local humanoid = script.Parent
local npc = humanoid.Parent
local primaryPart = npc.PrimaryPart or npc:WaitForChild("HumanoidRootPart")
local intValue = Instance.new("IntValue")
local npcSize = npc:GetExtentsSize()
local path = PathfindingService:CreatePath({
AgentCanJump = true,
AgentHeight = npcSize.Y,
AgentRadius = if npcSize.X < npcSize.Z then npcSize.Z / 2 else npcSize.X / 2})
local function getNearestPlayer(): Player?
local currentDistance = TARGET_DISTANCE
local nearestPlayer = nil
for _, player in Players:GetPlayers() do
if player.Character then
local distance = player:DistanceFromCharacter(primaryPart.Position)
if distance > STOP_DISTANCE and distance <= currentDistance then
currentDistance = distance
nearestPlayer = player
end
end
end
return nearestPlayer
end
local function npcMoveTo(targetPosition: Vector3)
path:ComputeAsync(primaryPart.Position, targetPosition)
if path.Status == Enum.PathStatus.Success then
for _, waypoint in path:GetWaypoints() do
humanoid.Jump = (waypoint.Action == Enum.PathWaypointAction.Jump)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
intValue.Changed:Connect(function(value: number)
if value == 0 then
humanoid:MoveTo(primaryPart.Position) -- This should stop the npc
else
local player = Players:GetPlayerByUserId(value)
if player and player.Character then
npcMoveTo(player.Character:GetPivot().Position)
end
end
end)
RunService.PostSimulation:Connect(function()
local player = getNearestPlayer()
intValue.Value = if player then player.UserId else 0
end)