Hello! So I have a script where if the npc sees the player it chases it. But the npc only sees you if your really close. How do I fix that and make him see far?
script not localscript:
local Freddy = script.Parent
local humanoid = Freddy.Humanoid
Freddy.PrimaryPart:SetNetworkOwner(nil)
local walk = script:WaitForChild('WalkAnim')
local sprint = script:WaitForChild('RunAnim')
local humanoid = script.Parent:WaitForChild('Humanoid')
local walkAnim = humanoid:LoadAnimation(walk)
local sprintAnim = humanoid:LoadAnimation(sprint)
game.ReplicatedStorage.RemoteEvents.Win.OnServerEvent:Connect(function()
script.Parent:Destroy()
end)
game.ReplicatedStorage.RemoteEvents.NightStart.OnServerEvent:Connect(function()
local function canSeeTarget(target)
local origin = Freddy.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - Freddy.HumanoidRootPart.Position).unit * 150
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, Freddy)
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 = 150
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (Freddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local PathfindingService = game:GetService("PathfindingService")
local pathParams = {
["AgentHeight"] = 8.3,
["AgentRadius"] = 3.5,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(Freddy.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (Freddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 2.5 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
walkAnim:Stop()
sprintAnim:Stop()
--Animation plays in jumpscarescript
end
end
local debouce = false
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
print("TARGET FOUND", target.Name)
attack(target)
if debouce == false then
debouce = true
script["FNAF 1 - Bonnie/Chica at door sound"]:Play()
script.ChaseMusic:Play()
game.Lighting.FogColor = Color3.new(0, 0, 0)
game.Lighting.FogEnd = 30
script.Parent.Eye.Light.Color = Color3.new(1, 0, 0)
script.Parent.Eye2.Light.Color = Color3.new(1, 0, 0)
script.Parent.Eye.Color = Color3.new(1, 0, 0)
script.Parent.Eye2.Color = Color3.new(1, 0, 0)
walkAnim:Stop()
sprintAnim:Play()
script.Parent.Humanoid.WalkSpeed = 22
end
break
else
script.ChaseMusic:Stop()
game.Lighting.FogColor = Color3.new(0, 0, 0)
game.Lighting.FogEnd = 75
script.Parent.Humanoid.WalkSpeed = 11
script.Parent.Eye.Light.Color = Color3.new(1, 1, 1)
script.Parent.Eye2.Light.Color = Color3.new(1, 1, 1)
script.Parent.Eye.Color = Color3.new(1, 1, 1)
script.Parent.Eye2.Color = Color3.new(1, 1, 1)
debouce = false
sprintAnim:Stop()
walkAnim:play()
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(destination.Position - (Freddy.HumanoidRootPart.CFrame.LookVector * 10))
end
end
function patrol()
local waypoints = script.Parent.WayPoints:GetChildren()
local randomNum = math.random(1, #waypoints)
script.Parent.Hitbox.action_footstep:Play()
walkTo(waypoints[randomNum])
end
while wait(0.05) do
patrol()
end
end)