How do I make it so NPCs/outfits load only when close?

I found that having too many clothing NPCs/outfits in your game makes it laggy and takes quite a while to load in.
How would I make it so that you can only see outfits that are close to you and the rest will just be blank/unloaded dummies?
Example here:

you can use this script i made a while back for this specific reason, make sure you give each of the rig models a Tag, in this case its “OutfitDisplay” but you can make it whatever you want.

Make sure to have a folder where the outfits are going to disappear to, and a folder inside workspace with the outfits.

-- lv_ver
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local outfitsFolder = Workspace:FindFirstChild("Outfits")
local reserveOutfitsFolder = ReplicatedStorage:FindFirstChild("ReserveOutfits")
local visibilityDistance = 60 -- change distance as you like

local function updateOutfitVisibility()
	local player = Players.LocalPlayer
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

	RunService.Heartbeat:Connect(function()
		local outfits = CollectionService:GetTagged("OutfitDisplay")
		for _, outfit in ipairs(outfits) do
			if outfit:IsA("Model") and outfit:FindFirstChild("HumanoidRootPart") then
				local outfitRoot = outfit.HumanoidRootPart
				local distance = (humanoidRootPart.Position - outfitRoot.Position).Magnitude

				if distance > visibilityDistance then
					outfit.Parent = reserveOutfitsFolder
				else
					outfit.Parent = outfitsFolder
				end
			end
		end
	end)
end

updateOutfitVisibility()
1 Like

Thanks, I already made this script but the lag spikes from constantly unloading and loading the characters was worse than the sustained lag of all the avatars in the game

1 Like

Where are the rigs being stored? I’d recommend ReplicatedStorage or ServerStorage, this seemed to make my game flawless in terms of performance.

I’d also recommend making a loading screen, seemed to help with performance issues on my end as well.