Which has my own piggy character in it but I can’t seem to make him jump when he gets stuck when moving to the nearest player.
Could anyone help?
Here is the follow code:
local npcHRP = script.Parent.HumanoidRootPart
local function GetNearestPlayer(minimumDistance)
local closestMagnitude = minimumDistance or math.huge
--minimumDistance is a number in studs
local closestPlayer
for i,v in next, game.Players:GetPlayers() do
local Character = v.Character
if (Character) then
local humanoid = Character.Humanoid
local HRP = Character.HumanoidRootPart
if (humanoid.Health > 0) then
local mag = (npcHRP.Position - HRP.Position).Magnitude
if (mag <= closestMagnitude) then
closestPlayer = v
closestMagnitude = mag
end
end
end
end
return closestPlayer
end
while wait() do
local nearPlr = GetNearestPlayer(70)
pcall(function()
script.Parent.Humanoid:MoveTo(nearPlr.Character.HumanoidRootPart.Position, nearPlr.Character.HumanoidRootPart)
end)
end
local npcHRP = script.Parent.HumanoidRootPart
local PathFindingServ = game:GetService("PathfindingService")
local npcH = script.Parent.Humanoid
local npcT = script.Parent.Torso
local function GetNearestPlayer(minimumDistance)
local closestMagnitude = minimumDistance or math.huge
--minimumDistance is a number in studs
local closestPlayer
for i,v in next, game.Players:GetPlayers() do
local Character = v.Character
if (Character) then
local humanoid = Character.Humanoid
local HRP = Character.HumanoidRootPart
if (humanoid.Health > 0) then
local mag = (npcHRP.Position - HRP.Position).Magnitude
if (mag <= closestMagnitude) then
closestPlayer = v
closestMagnitude = mag
end
end
end
end
return closestPlayer
end
while wait() do
local nearPlr = GetNearestPlayer(70)
pcall(function()
local path = PathFindingServ:CreatePath()
path:ComputeAsync(npcT.Position, nearPlr.Character.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
for _, wp in pairs(waypoints) do
if wp.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
print(wp.Action)
script.Parent.Humanoid:Move(wp.Position)
script.Parent.Humanoid.MoveToFinished:Wait(2)
end
script.Parent.Humanoid:MoveTo(nearPlr.Character.HumanoidRootPart.Position, nearPlr.Character.HumanoidRootPart)
end)
end
But the NPC/piggy just wants to not live and goes to the edge of the map and falls off.
local npcHRP = script.Parent.HumanoidRootPart
local PathFindingServ = game:GetService("PathfindingService")
local npcH = script.Parent.Humanoid
local npcT = script.Parent.Torso
local function GetNearestPlayer(minimumDistance)
local closestMagnitude = minimumDistance or math.huge
--minimumDistance is a number in studs
local closestPlayer
for i,v in next, game.Players:GetPlayers() do
local Character = v.Character
if (Character) then
local humanoid = Character.Humanoid
local HRP = Character.HumanoidRootPart
if (humanoid.Health > 0) then
local mag = (npcHRP.Position - HRP.Position).Magnitude
if (mag <= closestMagnitude) then
closestPlayer = v
closestMagnitude = mag
end
end
end
end
return closestPlayer
end
while wait() do
local nearPlr = GetNearestPlayer(70)
pcall(function()
local path = PathFindingServ:CreatePath()
path:ComputeAsync(npcT.Position, nearPlr.Character.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
for _, wp in pairs(waypoints) do
local part = Instance.new("Part")
if wp.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
print(wp.Action)
script.Parent.Humanoid:Move(wp.Position)
script.Parent.Humanoid.MoveToFinished:Wait()
end
end)
end
New script but makes the NPC all juttery and delayed:
local npcHRP = script.Parent.HumanoidRootPart
local PathFindingServ = game:GetService("PathfindingService")
local npcH = script.Parent.Humanoid
local npcT = script.Parent.Torso
local function GetNearestPlayer(minimumDistance)
local closestMagnitude = minimumDistance or math.huge
--minimumDistance is a number in studs
local closestPlayer
for i,v in next, game.Players:GetPlayers() do
local Character = v.Character
if (Character) then
local humanoid = Character.Humanoid
local HRP = Character.HumanoidRootPart
if (humanoid.Health > 0) then
local mag = (npcHRP.Position - HRP.Position).Magnitude
if (mag <= closestMagnitude) then
closestPlayer = v
closestMagnitude = mag
end
end
end
end
return closestPlayer
end
while wait() do
local nearPlr = GetNearestPlayer(70)
pcall(function()
local path = PathFindingServ:CreatePath()
path:ComputeAsync(npcT.Position, nearPlr.Character.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
for _, wp in pairs(waypoints) do
local part = Instance.new("Part")
if wp.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid.Jump = true
end
print(wp.Action)
script.Parent.Humanoid:MoveTo(wp.Position)
wait(0.05)
end
end)
end