i’m trying for npc to update SeesPlayer Value to true but if there is more than 2 players in the server he will never update that value more than once
wait(game.Loaded)
local PathfindingService = game:GetService("PathfindingService")
local Bobby = script.Parent
local humanoid = Bobby:WaitForChild("Humanoid")
local Humanoid_Root_Part = Bobby:WaitForChild("HumanoidRootPart")
Humanoid_Root_Part:SetNetworkOwner(nil)
local pathParams = {
AgentHeight = 5,
AgentRadius = 3,
AgentCanJump = true,
}
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {Bobby}
local lastPos
local RANGE = 150
local DAMAGE = 100
local function canSeeTarget(target)
local origin = Humanoid_Root_Part.Position
local direction = (target:WaitForChild("HumanoidRootPart").Position - Humanoid_Root_Part.Position).Unit * RANGE
local ray = workspace:Raycast(origin, direction, rayParams)
if ray then
if ray.Instance:IsDescendantOf(target) then
script.SeesTarget.Value = true
return true
else
script.SeesTarget.Value = false
return false
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = RANGE
local nearestTarget
for i, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (Humanoid_Root_Part.Position - target:WaitForChild("HumanoidRootPart").Position).Magnitude
if canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
print("i see ".. target.Name)
else
print("i don't see ".. target.Name)
end
end
end
return nearestTarget
end
local function getPath(destination)
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(Humanoid_Root_Part.Position, destination.Position)
return path
end
local function attack(target)
local distance = (Humanoid_Root_Part.Position - target:WaitForChild("HumanoidRootPart").Position).Magnitude
local debounce = false
local player = game.Players:GetPlayerFromCharacter(target)
if distance > 2 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
game.ReplicatedStorage.Remotes.BobbyDeathEvent:FireClient(player)
Bobby.Head.JumpScare:Play()
task.wait(1.3)
target.Humanoid.Health -= DAMAGE
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(path:GetWaypoints()) do
path.Blocked:Connect(function()
path:Destroy()
getPath(destination)
end)
local target = findTarget()
if target and target.Humanoid.Health > 0 then
lastPos = target:WaitForChild("HumanoidRootPart").Position
attack(target)
break
else
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
if lastPos then
humanoid:MoveTo(lastPos)
humanoid.MoveToFinished:Wait()
lastPos = nil
break
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
else
return
end
end
local function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while task.wait(0.01) do
patrol()
if humanoid.WalkSpeed > 10 then
Bobby.HumanoidRootPart.Walking.Playing = false
Bobby.HumanoidRootPart.Running.Playing = true
else
Bobby.HumanoidRootPart.Walking.Playing = true
Bobby.HumanoidRootPart.Running.Playing = false
end
end