So my game needs around 30 - 60 NPC's that just walk around randomly. So this is the script that makes the NPC walk randomly. But I want to control multiple NPC's with just 1 script. So I need help to use collection service.
local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local Torso = script.Parent:WaitForChild("Torso")
function walkRandomly()
local xRand = math.random(-75,100)
local zRand = math.random(-75,100)
local goal = myRoot.Position + Vector3.new(xRand,0,zRand)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position, goal)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
myHuman:MoveTo(waypoint.Position)
local timeOut = myHuman.MoveToFinished:Wait(1)
if not timeOut then
-- got stuck
myHuman.Jump = true
walkRandomly()
end
end
else
--path failed
wait(1)
walkRandomly()
end
end
Torso.Touched:Connect(function(obj)
if not obj.Parent:FindFirstChild("Humanoid") and math.random(1,24) == 24 then
myHuman.Jump = true
end
end)
function main()
myHuman.WalkSpeed = 16
walkRandomly()
end
while wait(0.1) do
if myHuman.Health < 1 then
break
end
main()
end
