You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve?
I would like the npc to be able to jump up and attack the found player -
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 -
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