-
What do you want to achieve?
I’m trying to make a npc to chase the player -
What is the issue?
I don’t know how to manage the while loop -
What solutions have you tried so far?
I tried lots of things but couldn’t fix my problem
The script I use :
local Rig = workspace.Hunter
local PathFinding = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local Players = game.Players:GetPlayers()
local Start = Rig.HumanoidRootPart.Position
local End = workspace.End
local Range = 80
local Mode
local ActualTarget
local ActualTarget = 1
local ActualMode
--Rig.Humanoid.JumpPower = 100
local Path = PathFinding:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = true,
--Costs = {
-- --Plastic = math.huge,
-- --Ice = 0
--}
}
)
local function Trace(waypoint)
local Part = Instance.new("Part", workspace)
Part.Position = waypoint.Position + Vector3.new(0,1,0)
Part.Size = Vector3.new(3,3,3)
Part.Anchored = true
Part.CanCollide = false
Part.Color = Color3.new(0.447059, 1, 0.0235294)
end
local function SearchForTarget()
--print("searching..")
for i, plr in ipairs(game.Players:GetPlayers()) do
plr = workspace:WaitForChild(plr.Name)
if (plr:WaitForChild("HumanoidRootPart").Position - Rig.HumanoidRootPart.Position).Magnitude <= Range then
--print("found !")
return plr
end
--print("not found ! : ", (plr.HumanoidRootPart.Position - Rig.HumanoidRootPart.Position).Magnitude)
end
end
local function CheckDistance(arg1)
local Treshold = 10
--print(arg1)
local Magnitude = (Rig.HumanoidRootPart.Position - arg1.Position).Magnitude
if Magnitude <= Treshold then
return true
end
end
local function CheckRange(target)
if (target.Position - Rig.HumanoidRootPart.Position).Magnitude <= Range then
return true
else
return false
end
end
local reset = false
local function Recalculate (Target)
Path:ComputeAsync(Rig.HumanoidRootPart.Position, Target.Position)
end
local function FindMode(target)
--if not CheckRange() then return "no range" end
if CheckDistance(target) then
return "Move"
else
return "PathFinding"
end
end
local function StopNavigation(actualTarget)
local ActualMode = ActualMode
local ShouldMode = FindMode(actualTarget)
--print(ActualMode," = ", ShouldMode)
if ActualMode ~= ShouldMode then
return true
else
return false
end
end
local STOP = false
local function stop()
STOP = true
end
local function Target(target, mode)
--print("choosen mode : ",mode)
print("w")
if mode == "PathFinding" then
Recalculate(target)
--print("path")
local Waypoints = Path:GetWaypoints()
if Path.Status ~= Enum.PathStatus.Success then return end
--Verify if the closest target changed or it needs to stop pathfinding and move
--PathFinding system
for i, waypoint in pairs(Waypoints) do
print("moving to : ",i)
if waypoint.Action == Enum.PathWaypointAction.Jump then
Rig.Humanoid.Jump = true
end
Rig.Humanoid:MoveTo(waypoint.Position)
Rig.Humanoid.MoveToFinished:Wait()
end
elseif mode == "Move" then
Rig.Humanoid:MoveTo(target.Position)
end
end
local new
local IsRunning = false
while task.wait(0.1) do
if IsRunning == true then
coroutine.yield(new)
end
local PotentialTarget = SearchForTarget()
if not PotentialTarget then continue end
PotentialTarget = PotentialTarget:WaitForChild("HumanoidRootPart")
ActualMode = FindMode(PotentialTarget)
--Target(PotentialTarget,ActualMode)
--Apply others to the target found : good
--Check the mode : good
--Give the mode to the target
--Apply the mode
--idk ?
--
--
new = coroutine.create(function()
Target(PotentialTarget, ActualMode)
end)
IsRunning = true
end
How should I procide for the final loop ?