So I have this npc and in this specific part of the script I want it so that once the npc has found a player to chase the npc will have a timer that goes down whenever the focused player moves out of its pov and when the timer reaches 0 the npc gives up and goes back to its waypoints but if the focused player went back into the npcs pov before the timer reached 0 then the timer would go back up to 500 and keep chasing the player.
local castRate = 24
local castRadius = 32
local updatePOVTime = 0.15
local updateTargetLocationTime = 0.1
local showRaycasts = true
local lockedIn = false
local focusedPlayer
local filter = {Dummy, Workspace.povVisuals}
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = filter
local giveUpChaseTime = 500
local giveUpChaseTimer = giveUpChaseTime
while true do
for i = 1, castRate do
if lockedIn then continue end
local step = i * (90 / castRate)
local _end = (finish.CFrame * CFrame.Angles(0, math.rad(step), 0) * CFrame.new(25, 0, -castRadius))
if showRaycasts then
local visualizePart = Instance.new("Part", Workspace.povVisuals)
visualizePart.BrickColor = BrickColor.new("Bright red")
visualizePart.FormFactor = "Custom"
visualizePart.Material = "Neon"
visualizePart.Transparency = 0.25
visualizePart.Anchored = true
visualizePart.Locked = true
visualizePart.CanCollide = false
visualizePart.Size = Vector3.new(0.1, 0.1, castRadius)
visualizePart.CFrame = CFrame.new(start.CFrame.p, _end.p) * CFrame.new(0, 0, -castRadius / 2)
end
local ray = Workspace:Raycast(start.CFrame.p, (_end.p - start.CFrame.p).unit * castRadius, params)
if ray and (Players:GetPlayerFromCharacter(ray.Instance.Parent) or Players:GetPlayerFromCharacter(ray.Instance.Parent.Parent)) then
local foundPlayer = (Players:GetPlayerFromCharacter(ray.Instance.Parent) or Players:GetPlayerFromCharacter(ray.Instance.Parent.Parent))
print("I SEE YOU ".. string.upper(foundPlayer.DisplayName))
lockedIn = true
focusedPlayer = foundPlayer
task.spawn(function()
while lockedIn do
if not focusedPlayer then
lockedIn = false
task.wait(2)
moveRandomWaypoint()
break
end
Path:Run(focusedPlayer.Character.HumanoidRootPart)
for i2 = 1, castRate do
local step2 = i2 * (90 / castRate)
local _end2 = (finish.CFrame * CFrame.Angles(0, math.rad(step2), 0) * CFrame.new(25, 0, -castRadius))
if showRaycasts then
local visualizePart = Instance.new("Part", Workspace.povVisuals)
visualizePart.BrickColor = BrickColor.new("Dark blue")
visualizePart.FormFactor = "Custom"
visualizePart.Material = "Neon"
visualizePart.Transparency = 0.25
visualizePart.Anchored = true
visualizePart.Locked = true
visualizePart.CanCollide = false
visualizePart.Size = Vector3.new(0.1, 0.1, castRadius)
visualizePart.CFrame = CFrame.new(start.CFrame.p, _end2.p) * CFrame.new(0, 0, -castRadius / 2)
end
local ray2 = Workspace:Raycast(start.CFrame.p, (_end2.p - start.CFrame.p).unit * castRadius, params)
if ray2 then
if (Players:GetPlayerFromCharacter(ray2.Instance.Parent) or Players:GetPlayerFromCharacter(ray2.Instance.Parent.Parent)) then
print("player is in range")
end
else
print("player is out range")
end
if giveUpChaseTimer <= 0 then
print("WHERE DID YOU GO?")
focusedPlayer = nil
giveUpChaseTimer = giveUpChaseTime
end
end
task.wait(updateTargetLocationTime)
end
end)
end
end
task.delay(0.3, function()
for _, v in Workspace.povVisuals:GetChildren() do
v:Destroy()
end
end)
task.wait(updatePOVTime)
end
And here is a video:
You can see that it works when the player isn’t in the range of the blue lines but once they are, both prints get triggered.