How would I make a NPC jump when using :MoveTo()

Hi everybody!

How would I make my NPC jump when using :MoveTo() when the NPC is stuck on a jump-able object (or terrain).

I currently have this game: Free In-Game Admin! [Free Private Servers!] - Roblox

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
1 Like

You use Humanoid:ChangeState()

2 Likes

But how would I know if the NPC needs to jump when it is stuck?

You would use pathfinding and check the action of one of the waypoints.

Here is a video explaining it:

4 Likes

Yeah but how would I make it path find the player?

I mean, I don’t know how I would make it do this

You could just set the jump bool in Humanoid to true (I forgot its name). It will go back to false on its own too.

1 Like

I know, I am currently testing what @SpacialEthanRB said.

You could set the destination to the player’s HumanoidRootPart. If you put it into a run service function, you can get it following you non stop!

Wait… here is my new follow script:

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.

It doesn’t jump when I put a brick in front of it either

It has only 1 waypoint too which is where the npc starts moving.

Just follow the tutorial mentioned by @SpacialEthanRB, but use the player’s humanoidRootPart as the destination.

I did! Did you not see the code I sent? Please help :confused:

Here is the code again:

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

I have posted a new post for this issue, see this: PathfindingService is making NPC jump off the edge of map instead of chasing player

You’re doing Humanoid:Move(wp.Position), You should be doing Humanoid:MoveTo(wp.Position)

Also, Humanoid.MoveToFinished:Wait() ends up studdering, you might wanna look into using magnitude instead of .MoveToFinished.

1 Like

Ah thanks, how would I use the magnitude though?

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

When he is in water, he doesn’t jump to come out of it.