How to make my item spawner more server efficient?

My item spawner spawns in items that players could sell for cash. I’m afraid that having a bunch of these on my map will strain the server and cause immense lag. If that is true, how can I improve my code to run efficiently on the server?
6d88c4f1f76fa9289d2abf37e2022ee2

local Players = game.Players
local ServerStorage = game.ServerStorage
local ItemsFolder = ServerStorage.Items
local TrinketsFolder = ItemsFolder.Trinkets
local playersInArea = {}
local spawnDistance = 50
local trinketSpawnTime = 10
local currentTrinket

while true do
	for _, plr in pairs(Players:GetPlayers()) do
		local char = plr.Character
		if char then
			local root = char.HumanoidRootPart
			local distanceFromTrinketSpawn = (root.Position - script.Parent.Position).magnitude
			if distanceFromTrinketSpawn <= spawnDistance then
				playersInArea[plr.Name] = plr
			elseif distanceFromTrinketSpawn > spawnDistance and playersInArea[plr.Name] ~= nil then
				playersInArea[plr.Name] = nil
			end
		end
	end
	
	local plrCount = 0
	for plrName in pairs(playersInArea) do
		plrCount = plrCount + 1
	end
	
	print(plrCount .. " players in area")
	if plrCount > 0 then				
		if currentTrinket then
			repeat wait(5) until #script.Parent:GetChildren() == 1
			currentTrinket = nil
			wait(trinketSpawnTime)
		else
			-- Clone a random trinket/artifact/spell into here
			local chance = math.random(1, 100)
			if chance > 0 and chance <= 100 then -- 100% chance for trinket for the time being
				print("spawn trinket")
				local trinketNum = math.random(1, #TrinketsFolder:GetChildren())
				local trinket
				for i, v in pairs(TrinketsFolder:GetChildren()) do
					if i == trinketNum then
						trinket = v:Clone()
						break
					end
				end
				trinket.Parent = script.Parent
				trinket.CFrame = script.Parent.CFrame
				currentTrinket = trinket
			end
		end
			
	end	
	wait(5)
end
2 Likes

Could you explain the concept of the game? If the main part is for the players to just spawn items in to sell then it might strain the server especially if certain items are large. However, if spawning items isn’t the main point of the game and is something to do on the side than it shouldn’t be much of an issue.

There are also a lot of other factors such as:
-Map Size
-Server Size
-Amount of scripts running at once
-Etc

Could you display the context of this script? It would make code review much easier if we can see the explorer view of the script to see where it is.

You should give us more information like is the trinket spawning everywhere around the map? Is it only in certain places around the map? You could just have triggers in those areas that will only start running and stop if there’s no more activity in those areas. We need more context.

The trinkets spawn at parts called “Trinket Spawns”. Those trinket spawns are placed all over a map, fairly large. I was able to reduce the code to a third of it’s length after a friend notified me that he saw my post. As for the server size, it was going to around 24 players. Another thing recently crossed my mind, which was that the same code was inside each trinket spawn part. Would using collection service make it run better on the server?

1 Like

Yes! Definitely use CollectionService over iterating through a game object’s descendants or duplicating the script, within reason.

Seeing that your current code in the first post is outdated, I won’t review it; I’d be more than happy to review your new code in due time—Other members of the community can definitely give you more insight with the newer snippet as well.

1 Like