Hello! I’m messing around with collection service trying to make NPCs for my game. Thing is, i dont know what the hell im doing
Im trying to make the NPCs face the nearest player at all times, however, when i run the script i wrote only 1 of the NPCs follows the player while the others just stand there as if to mock me.
local collectionService = game:GetService("CollectionService")
local players = game:GetService("Players")
local maxDistance = 30
for _, funnyMan in pairs(collectionService:GetTagged("Test")) do
local humanoid = funnyMan:WaitForChild("Humanoid") or funnyMan.Humanoid
while true do
wait(0.01)
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
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
end
end
can someone tell me whats wrong with my script? thank.
To add on what @dthecoolest said, you can wrap the NPC look at logic in a local function without the while loop and connect it to RunService.Stepped instead. Sure, you can no longer use wait or task.wait, but it’ll still run per NPC without stopping the initial for loop.
Actually that would be less performant, as Stepped fires 1/x times per seconds where x is the user’s frame rate so typically it would run 1/60 times per seconds which is really expensive for NPC logic especially if you were to add more NPCs. On the other hand, a while loop which runs every 0.01 seconds i.e 1/10th of second is much more reasonable for this use case. Nvm I just realized that the while loop runs every 1/100th 1/30th of second which is overkill, but ye anyways OP change the frequency of the while loop to run every 0.1 seconds
local collectionService = game:GetService("CollectionService")
local players = game:GetService("Players")
local maxDistance = 30
local function funnyFunc(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
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
end
for _, funnyMan in pairs(collectionService:GetTagged("Test")) do
local humanoid = funnyMan:WaitForChild("Humanoid") or funnyMan.Humanoid
while task.wait() do
funnyFunc(funnyMan)
end
end
collectionService:TagAdded("tag"):Connect(funnyFunc())
local collectionService = game:GetService("CollectionService")
local players = game:GetService("Players")
local maxDistance = 30
local function funnyFunc(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
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
end
for _, funnyMan in pairs(collectionService:GetTagged("Test")) do
funnyFunc(funnyMan)
end
collectionService:TagAdded("Test"):Connect(funnyFunc)
I wrote this but its returning 09:25:25.489 TagAdded is not a valid member of CollectionService "CollectionService" - Server - Script:30. Is that how we use TagAdded?