Hello everybody.
Lately i found code which seemed to be answering my question, but it actually doesnt and i dont know what is the cause. I made script and put it inside:
local dummy = script.Parent
local humanoid = dummy:WaitForChild("Humanoid")
while true do
wait(10)
humanoid:MoveTo(Vector3.new(math.random(-20,20),0,math.random(-20,20)))
end
local head = script.Parent.Head
local HRP = script.Parent.HumanoidRootPart
local Hum = script.Parent.Humanoid
local FOV = 60 -- field of view of the player
local range = math.huge -- basically how far a player can look at the npc until they stop
local PlayerService = game:GetService("Players")
local function GetCharacters() -- will make it so other characters wont block out the npc from your fov
local Characters = {}
for _, b in PlayerService:GetPlayers() do
table.insert(Characters, b.Character)
end
return Characters
end
local function CheckLineOfSight(Start, End) -- raycasting, basically makes it so the npc isnt detected thru walls
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {script.Parent, GetCharacters()}
Params.FilterType = Enum.RaycastFilterType.Exclude
local Raycast = game.Workspace:Raycast(Start, End - Start, Params)
if Raycast then
return false
else
return true
end
end
function detectNpc(target)
local Character = target.Parent
if Character then
if Character.Head then
local NPCToCharacter = (head.Position - Character.Head.Position).Unit
local NPCLookVector = Character.Head.CFrame.LookVector
local DotProduct = NPCToCharacter:Dot(NPCLookVector)
local Angle = math.deg(math.acos(DotProduct))
local Distance = (Character.HumanoidRootPart.Position - HRP.Position).Magnitude
if Angle <= FOV and Distance <= range and CheckLineOfSight(Character.Head.Position, head.Position) then
Hum.WalkSpeed = 0
-- looked
else
Hum.WalkSpeed = 16
-- not looked
end
end
end
end
right now im not sure what’s happening. I tried this code on normal dummy and it doesnt work too, but here i want to implement it on custom character, but it probably doesnt matter. Could someone tell me whats really not working about this script?