Monster NPC not jumping upto Player

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I would like the npc to be able to jump up and attack the found player

  2. What is the issue?


    The npc is not jumping up to attack the player and i think i need help with triggering the changestate earlier

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes, I have looked up many solutions and they all pretty much say to add the changestate into the waypoint loop which i have done.
    I also checked autojump is enabled, change jump height for the npc, turned on/off jump power.
    Moved the changestate up and down the waypoint loop and even tried moving it to different locations in the script

--Services
local pathfindingService = game:GetService("PathfindingService")

--Remote Events
local JumpscareEvent = game.ReplicatedStorage.RemoteEvent.JumpscareEvent

--Varibles
local Anim = Instance.new("Animation") Anim.AnimationId = "rbxassetid://9249753812" 
local AttackAnim = Instance.new("Animation") AttackAnim.AnimationId = "rbxassetid://9366469168"
local Chase = game.Workspace.Monsters.JohnFilingRoomChase.Humanoid:LoadAnimation(Anim) 
local Kill = game.Workspace.Monsters.JohnFilingRoomChase.Humanoid:LoadAnimation(AttackAnim) 
local Monster = game.Workspace.Monsters.JohnFilingRoomChase.Humanoid
local MonsterBody = game.workspace.Monsters.JohnFilingRoomChase.HumanoidRootPart
local RootPart = game.Workspace.Monsters.JohnFilingRoomChase:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("UpperTorso")
local Primarypart = game.Workspace.Monsters.JohnFilingRoomChase
local Footsteps = game.Workspace.Monsters.JohnFilingRoomChase.LowerTorso.FootStomps

--Start Moving Condition
repeat wait() until game.Workspace.Scripts.Objective:FindFirstChild("InformationFoundTrigger") == nil
Primarypart.PrimaryPart:SetNetworkOwner(nil) 
Footsteps:Play()

--Functions
local function canSeeTarget(target)
	local origin = MonsterBody.Position
	local direction = (target.HumanoidRootPart.Position - MonsterBody.Position).unit * 40
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, Monster)
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 90
	local nearestTarget = nil

	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (MonsterBody.Position - target.HumanoidRootPart.Position).Magnitude

			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	return nearestTarget
end

local function attack(target)
	local distance = (MonsterBody.Position - target.HumanoidRootPart.Position).Magnitude
	local Player = game.Players:GetPlayerFromCharacter(target)
	
	if distance > 8 then
		Monster:MoveTo(target.HumanoidRootPart.Position)
		Monster.MoveToFinished:Wait()
		else
		if distance <= 8 then
			Monster:MoveTo(target.HumanoidRootPart.Position)
			target.Humanoid.Health = 0
			Kill:Play()
			Kill:AdjustSpeed(3)
			JumpscareEvent:FireClient(Player)
			wait(1.5)
		end
	end
end

local function getPath(goal)
	local pathParams = {
		["AgentHeight"] = 9,
		["AgentRadius"] = 12,
		["AgentCanJump"] = true}
	
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(RootPart.Position, goal.Position)

	return path
end

local function walkTo(goal)
	local path = getPath(goal)

	if path.Status == Enum.PathStatus.Success then

		for index, waypoint in pairs(path:GetWaypoints()) do
			
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				Monster:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				print("Target Found",target.Name)
				attack(target)
				break
			else
				print("Moving To",waypoint.Position)
				Monster:MoveTo(waypoint.Position)
				Monster.MoveToFinished:Wait()
			end
		end
		else
		Monster:MoveTo(goal.Position - (MonsterBody.CFrame.LookVector * 10))
	end
end	  

local function patrol()
	local waypoints = workspace.Scripts.Monster.FilingRoomWaypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while wait(0.3) do  
	patrol()
end

Chase:Play() -- This is a movement Animation Type

1 Like

I managed to figure out how to make it jump earlier but now because the IF statement is true when within the distance check the npc monster keeps jumping.

I cleaned up the script a little bit but does anyone know how to fix the ChangeState part?

autojump is disabled incase you’re wondering

local function attack(target)
	local distance = (MonsterBody.Position - target.HumanoidRootPart.Position).Magnitude
	local Player = game.Players:GetPlayerFromCharacter(target)
	
	if distance > 8 then
		Monster:MoveTo(target.HumanoidRootPart.Position)
		wait(1.2)
		Monster:ChangeState(Enum.HumanoidStateType.Jumping)
	else
		target.Humanoid.Health = 0
		Kill:Play()
		Kill:AdjustSpeed(3)
		JumpscareEvent:FireClient(Player)
	end
end

I think it should be something simple, something I’m not thinking about or seeing.
Perhaps something along the lines of this?

		if target.HumanoidRootPart.Position.Y > 6 then
			Monster:ChangeState(Enum.HumanoidStateType.Jumping)	
		end

I would replace those lines with something like this:

Monster.Jump = true

I can confirm both

Monster.Jump = true 

and

Monster:ChangeState(Enum.HumanoidStateType.Jumping)	

make the npc monster jump however I believe the issue is with either the findtarget section or attack section

I just need the npc to check if the target is up on something and the target distance is close then jump