i don’t believe sending the script is required, since my issue has nothing to do with the script.
i wish to make the path:createpath() function, take account the current jumppower of the npc.
currently when the npc jumps, it uses the correct jump power.
but either way here is the code: (I know it’s kinda messy, working on that rn)
local module = {}
local PathfindingService = game:GetService("PathfindingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local TweeenService = game:GetService("TweenService")
local Info = TweenInfo.new(0.2,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)
local Raycastparams = RaycastParams.new()
Raycastparams.FilterType = Enum.RaycastFilterType.Include
Raycastparams.FilterDescendantsInstances = {workspace.Map}
local Players = workspace:WaitForChild("Players")
local Enemies = workspace:WaitForChild("Enemies")
local abs = math.abs
-- Functions
local CheckLocation = function(Pos1,Pos2)
local Distance = (Pos1 - Pos2).Magnitude
local Raycast = workspace:Raycast(Pos1,-(Pos1 - Pos2).Unit * Distance,Raycastparams)
if Raycast then
return true
else
return false
end
end
local Distance = function(Pos1,Pos2)
local Distance = (Pos1 - Pos2).Magnitude
return Distance
end
local function CheckFloor(Hrt)
local Offset = -1
for i = 1,3 do
local Raycast = workspace:Raycast((Hrt.CFrame * CFrame.new(Offset,0,0)).Position,Hrt.CFrame.UpVector * -3.1,Raycastparams)
Offset += 1
if Raycast then
return true
end
end
end
local function createpath(Destination,Hrt,Hum,Range,Path)
if Destination == nil then
return
end
local Pos1 = Hrt.Position
local Pos2 = Destination.Position
if not CheckLocation(Pos1,Pos2) and Distance < Range then
return "Continue"
end
Pos2 = Destination.Position
local Success,Error = pcall(function()
--if (Pos2 - Hrt.Position).Magnitude > (Pos1 - Hrt.Position).Magnitude then Pos2 = Pos1 end
Path:ComputeAsync(Hrt.Position - Vector3.new(0, Hrt.Size.Y/0.75, 0),Pos2)
end)
if Path.Status == Enum.PathStatus.Success then
return Path:GetWaypoints()
end
end
local Types = {}
for i,v in pairs(script:GetChildren()) do
if v:IsA("ModuleScript") then
Types[v.Name] = require(v)
end
end
module.Setup = function(Character,Type)
local Hrt = Character:WaitForChild("HumanoidRootPart")
local Head = Character:WaitForChild("Head")
local Hum = Character:WaitForChild("Humanoid")
local Animator = Hum:WaitForChild("Animator")
local FindPath
local Path = PathfindingService:CreatePath(
{AgentRadius = 2.5, AgentHeight = 5,AgentCanJump = true,AgentCanClimb = true,WaypointSpacing = 3}
)
local StatusHandler = _G.StatusHandler
for i,v in pairs(Character:GetDescendants()) do
if v:IsA("Part") or v:IsA("BasePart") then
pcall(function()
v:SetNetworkOwner(nil)
end)
end
end
local Blocked = nil
local Reached = nil
local NextIndex = 1
local Changed = nil
local Waypoints = nil
local CurrentTarget = StatusHandler.CreateValue({Character = Character,Name = "CurrentTarget",Time = math.huge,Value = script})
CurrentTarget.Value = nil
local IsAttacking = StatusHandler.CreateValue({Character = Character,Name = "IsAttacking",Time = math.huge,Value = false})
local CanAttack = StatusHandler.CreateValue({Character = Character,Name = "CanAttack",Time = math.huge,Value = true})
local Targetting = StatusHandler.CreateValue({Character = Character,Name = "Targetting",Time = math.huge,Value = false})
local Died = false
local Shoot = 1
local Find
local Range = 0
local WhiteListFind = {}
local NewPath = nil
local Whitelist = {}
local AutoJump = false
local TargetWhitelist = {}
local LastReached = os.clock()
local AttackMod = Types[Type]
Range = AttackMod.Stats.Range
TargetWhitelist = AttackMod.Stats.Whitelist
local CheckWhitelist = function(Object)
if Object:FindFirstChild("Tags") == nil then print("notag") return false end
for i,v in pairs(TargetWhitelist) do
if Object.Tags:FindFirstChild(v) then
return true
end
end
return false
end
local FindPlayer = function(Character)
local Hrt = Character.PrimaryPart
local Closest = nil
local ToLoop = Players:GetChildren()
local Length = 0
for i,v in pairs(Whitelist) do
Length += 1
end
if Length >= #ToLoop then Whitelist = {} end
for i,v in pairs(Enemies:GetChildren()) do
table.insert(ToLoop,v)
end
for i,v in pairs(ToLoop) do
task.wait()
if v == Character or CheckWhitelist(v) then continue end
if v:FindFirstChild("Humanoid") == nil then continue end
if v.Humanoid.Health <= 0 then continue end
if Closest == nil and Whitelist[v] == nil then
Closest = v
elseif Closest then
local Dis1 = (Hrt.Position - v.PrimaryPart.Position).Magnitude
local Dis2 = (Hrt.Position - Closest.PrimaryPart.Position).Magnitude
if Dis1 < Dis2 and Whitelist[v] == nil then
Closest = v
end
end
end
return Closest
end
spawn(function()
if AttackMod then
spawn(function()
AttackMod.Setup(Character)
end)
end
end)
local function FollowPath(Args)
local Destination = Args.Destination
local Waypoints = Args.Waypoints
AutoJump = Args.AutoJump
if Waypoints ~= "Continue" then
for i,v in pairs(Waypoints) do
local Part = Instance.new("Part")
Part.Anchored = true
Part.Size = Vector3.new(0.5,0.5,0.5)
Part.CanCollide = false
Part.Transparency = 0.1
Part.Position = v.Position
Part.Parent = workspace.Effects
task.delay(0.5,function()
Part:Destroy()
end)
end
end
if Destination.Parent:FindFirstChild("SF") == nil then return end
local Pos1 = Hrt.Position
local Pos2 = Destination.Position
local WhiteListFind = {}
local NewPath = nil
local DistancePos = (Pos1 - Pos2).Magnitude
local End = false
if Blocked then
Blocked:Disconnect()
Blocked = nil
end
if Reached then
Reached:Disconnect()
Reached = nil
end
--Waypoints = createpath(Destination,Hrt,Hum,Range,Path)
local CurrentState = Hum:GetState()
if Waypoints == nil or CurrentState == Enum.HumanoidStateType.Climbing then task.wait(2) CurrentTarget.Value = nil return end
local HrtPos = Hrt.CFrame * CFrame.new(0,-0.4,0)
local Dir = HrtPos.Position - (HrtPos * CFrame.new(0,0,-1).Position)
local Raycast = workspace:Raycast(Hrt.Position,-Dir.Unit * Dir.Magnitude,Raycastparams)
if Raycast then
if CheckFloor(Hrt) then
Hum:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
CurrentTarget.Value = Destination.Parent
Blocked = Path.Blocked:Connect(function(BlockedIndex)
if BlockedIndex >= NextIndex then
print("lol blocked")
FindPath()
end
end)
local LastPos = Destination.Position
local DtAdd = 0
if not Changed then
Changed = RunService.Heartbeat:Connect(function(dt)
DtAdd += dt
local PrimaryFind
if CurrentTarget.Value then
PrimaryFind = CurrentTarget.Value.HumanoidRootPart
else
return
end
WhiteListFind = {}
local PrimaryFindPos = PrimaryFind.Position
if not IsAttacking.Value and Targetting.Value then
local HrtPos = Hrt.Position
local Pos = CFrame.lookAt(HrtPos,Vector3.new(PrimaryFindPos.X,HrtPos.Y,PrimaryFindPos.Z))
local Track = TweeenService:Create(Hrt,Info,{CFrame = Pos})
Track:Play()
end
if DtAdd < 0.2 then return end
DtAdd = 0
local Distance = (Hrt.Position - LastPos).Magnitude
if os.clock() - LastReached >= 0.8 and Distance > 1 then
LastReached = os.clock()
FindPath()
end
if AutoJump then
local HrtPos = Hrt.CFrame * CFrame.new(0,-0.4,0)
local Dir = HrtPos.Position - (HrtPos * CFrame.new(0,0,-1).Position)
local Raycast = workspace:Raycast(Hrt.Position,-Dir.Unit * Dir.Magnitude,Raycastparams)
local JumpDis = HrtPos.Position - PrimaryFindPos
if Raycast and Distance.Magnitude >= 1 then
Hum:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
local Headpos = Head.Position
local PrimaryHead = PrimaryFind.Parent.Head
local PrimaryHeadPos = PrimaryHead.Position
local Dir = (PrimaryHeadPos - Headpos)
local NewRay = workspace:Raycast(Headpos,Dir.Unit * Dir.Magnitude, Raycastparams)
local YNitude = math.abs(PrimaryFindPos.Y - Hrt.Position.Y)
local Jp = Hum.JumpPower^2 / (2*workspace.Gravity) * 1.25
if NewRay == nil and YNitude < Jp then
FollowPath({Destination = PrimaryFind,AutoJump = true,Waypoints = {[2] = {Position = PrimaryFind.Position}}})
return
end
WhiteListFind = {}
Find = FindPlayer(Character,WhiteListFind)
if Find and Find ~= CurrentTarget.Value then
FindPath()
end
if CurrentTarget.Value then
if CurrentTarget.Value.Humanoid.Health <= 0 then CurrentTarget.Value = nil end
if PrimaryFindPos ~= LastPos and (PrimaryFindPos - LastPos).Magnitude >= 10 then
NewPath = createpath(PrimaryFind,Hrt,Hum,Range,Path)
if NewPath then
FollowPath({Destination = PrimaryFind,Waypoints = NewPath})
elseif NewPath == nil then
CurrentTarget.Value = nil
end
end
end
LastPos = PrimaryFindPos
end)
end
if not Reached then
Reached = Hum.MoveToFinished:Connect(function(reached)
if CurrentTarget.Value == nil then return end
if Waypoints == nil then return end
local Pos1 = Hrt.Position
local Pos2 = CurrentTarget.Value.PrimaryPart.Position
local WaypointDex = Waypoints[NextIndex]
if WaypointDex == nil then return end
if not CheckLocation(Pos1,Pos2) and Distance(Pos1,Pos2) < Range then return end
if reached and NextIndex < #Waypoints then
NextIndex += 1
LastReached = os.clock()
Hum:MoveTo(WaypointDex.Position)
if Waypoints[NextIndex].Action == Enum.PathWaypointAction.Jump then
Hum:ChangeState(Enum.HumanoidStateType.Jumping)
end
Targetting.Value = false
else
FindPath()
end
end)
end
NextIndex = 2
if Waypoints[NextIndex] then
local Pos1 = Hrt.Position
local Pos2 = Destination.Position
if CheckLocation(Pos1,Pos2) or Distance(Pos1,Pos2) > Range then
if Waypoints[NextIndex].Action == Enum.PathWaypointAction.Jump then
if CheckFloor(Hrt) then
Hum:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
Hum:MoveTo(Waypoints[NextIndex].Position)
Targetting.Value = false
end
end
end
FindPath = function()
WhiteListFind = {}
repeat
Find = FindPlayer(Character,WhiteListFind)
if Find then
local PrimaryFind = Find.PrimaryPart
local PrimaryFindPos = PrimaryFind.Position
local Dir = (PrimaryFind.Parent.Head.Position - Character.Head.Position)
local NewRay = workspace:Raycast(Character.Head.Position,Dir.Unit * Dir.Magnitude, Raycastparams)
local YNitude = math.abs(PrimaryFindPos.Y - Hrt.Position.Y)
local Jp = Hum.JumpPower^2 / (2*workspace.Gravity)
if NewRay == nil and YNitude < Jp then
FollowPath({Destination = PrimaryFind,AutoJump = true,Waypoints = {[2] = {Position = PrimaryFind.Position}}})
return
end
NewPath = createpath(PrimaryFind,Hrt,Hum,Range,Path)
if NewPath then
FollowPath({Destination = PrimaryFind,Waypoints = NewPath})
elseif NewPath == nil then
WhiteListFind[Find] = true
end
end
task.wait()
until CurrentTarget.Value and NewPath
WhiteListFind = {}
end
FindPath()
-- attack
spawn(function()
while task.wait() do
if Died then
break
end
if CurrentTarget.Value == nil then
FindPath()
end
end
end)
spawn(function()
while task.wait(5) do
if Died then
break
end
FindPath()
end
end)
-- Died
Hum:GetPropertyChangedSignal("Health"):Connect(function()
if Hum.Health <= 0 then
if Changed then
Changed:Disconnect()
end
if Blocked then
Blocked:Disconnect()
end
if Reached then
Reached:Disconnect()
end
Died = true
end
end)
end
return module