Im raising 2 NPCs and making them follow me with 1 ServerScript using CollectionService but i am displeased by their constant tomfoolery and behavior. They do in fact follow me, il give them that, albeit in a very unappealing way as shown in this video.
and here is my script
local collectionService = game:GetService("CollectionService")
local players = game:GetService("Players")
local funnyMen = collectionService:GetTagged("Enemy")
local runService = game:GetService("RunService")
local pathFindingService = game:GetService("PathfindingService")
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = workspace.Map:GetChildren()
local maxDistance = 30
local function getClosestPlayer(funnyMan)
local nearestPlayer, nearestDistance
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
local distance = player:DistanceFromCharacter(funnyMan.HumanoidRootPart.Position)
if not character or
distance > maxDistance or
(nearestDistance and distance >= nearestDistance)
then
continue
end
nearestDistance = distance
nearestPlayer = player
end
if nearestPlayer then
return nearestPlayer
end
end
local function checkIfFov(funnyMan)
local head = funnyMan.Head.CFrame.LookVector
local vis = false
for _, player in pairs(players:GetPlayers()) do
local playerPos = player.Character:WaitForChild("HumanoidRootPart").Position
local distanceFromNPC = (funnyMan.HumanoidRootPart.Position - playerPos).magnitude
if distanceFromNPC <= 20 then
local NPCToPlayer = (funnyMan.Head.Position - playerPos).Unit
local dot = NPCToPlayer:Dot(head)
if dot <= -0.5 then
vis = true
return player
else
vis = false
end
end
end
end
local function checkIfCanSee(funnyMan, player)
raycastParams.FilterDescendantsInstances = {funnyMan, player.Character}
local rayDirection = player.Character.HumanoidRootPart.Position-funnyMan.HumanoidRootPart.Position
local raycast = workspace:Raycast(funnyMan.HumanoidRootPart.Position, rayDirection)
if raycast ~= nil then
return raycast.Instance
end
end
local function funnyFunc(funnyMan, nearestPlayer)
funnyMan.PrimaryPart.CFrame = CFrame.new(funnyMan.PrimaryPart.Position, Vector3.new(nearestPlayer.Character.PrimaryPart.Position.X, funnyMan.PrimaryPart.Position.Y, nearestPlayer.Character.PrimaryPart.Position.Z))
end
for i,v in pairs(funnyMen) do
local humanoid = v:WaitForChild("Humanoid")
local detectEvent = v:FindFirstChild("Detect")
local animator = humanoid:WaitForChild("Animator")
local idleAnim = Instance.new("Animation")
idleAnim.AnimationId = "rbxassetid://14185521194"
local idleAnimTrack = animator:LoadAnimation(idleAnim)
task.defer(function()
local detected = false
local hurtDetect = false
idleAnimTrack:Play()
runService.Heartbeat:Connect(function()
local closestPlayer = getClosestPlayer(v)
if closestPlayer then
local fov = checkIfFov(v)
if fov ~= nil then
local cansee = checkIfCanSee(v, fov)
if cansee:FindFirstChild("wall") then
else
funnyFunc(v, closestPlayer)
local path = pathFindingService:CreatePath()
path:ComputeAsync(v.PrimaryPart.Position, closestPlayer.Character.PrimaryPart.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
end
end
end
end
detectEvent.OnServerEvent:Connect(function()
if detected == false then
funnyFunc(v, closestPlayer)
detected = true
wait(5)
detected = false
end
end)
v:WaitForChild("Humanoid").HealthChanged:Connect(function()
if hurtDetect == false then
hurtDetect = true
funnyFunc(v, closestPlayer)
wait(5)
hurtDetect = false
end
end)
end)
end)
end
can someone tell me what’s wrong? Is this an issue outside of my control? Do i just leave them be? Am i being a bad mother? please help me, thank you.