Trying to make a good entity spawn/despawning system

So I’ve got a game which has a lot of entities, and its lagging a lot if all of them are spawned in. I’m trying to make a spawning/despawning system with different chunks. This doesn’t work and just causes more lag. Here’s my code

for i,v in pairs(game.Workspace.Spawns) do
local AI = require(game.ServerStorage.AIScripts:FindFirstChild(v.Name):Clone())
local chunk = Instance.new("Part",game.Workspace.Chunks)
chunk.Anchored = true
chunk.CanCollide = false
chunk.Transparency = 1
chunk.Position = v.Position
chunk.Size = Vector3.new(AI.Settings.ReturnIfFurtherThan*2,AI.Settings.ReturnIfFurtherThan*2,AI.Settings.ReturnIfFurtherThan*2)
chunk.Touched:Connect(function(thing)
	if thing.Parent:FindFirstChild("HumanoidRootPart") and game.Players:GetPlayerFromCharacter(thing.Parent) and not chunkTouching[chunk] then
		chunkTouching[chunk] = true
		while chunkTouching[chunk] do
			local plrInChunk = false
			for i,v in pairs(game.Players:GetPlayers()) do
				if v.Character and v.Character:FindFirstChild("HumanoidRootPart") then
					local touching = v.Character.HumanoidRootPart:GetTouchingParts()
					if table.find(touching,chunk) then
						plrInChunk = true
					end
				end
			end
			if not plrInChunk then
				if Spawns[chunk] then
					Spawns[chunk]:Destroy()
					Spawns[chunk] = nil
				end
				chunkTouching[chunk] = nil
				return
			end
			if Spawns[chunk] and Spawns[chunk].Parent then continue end
			local entity = game.ServerStorage.Entities:FindFirstChild(v.Name):Clone()
			entity.HumanoidRootPart.CFrame = v.CFrame + Vector3.new(0,5,0)
			entity.Parent = game.Workspace.Entities
			Spawns[chunk] = entity
			setupEntity(entity,v)
			wait(5)
		end
	end
end)
end

I get a script timeout: exhausted allowed execution time error on table.find(touching,chunks)
It only seems to error if I approach the boss which has a larger ReturnIfFurtherThan

2 Likes

Shouldn’t this be game.Workspace.Spawns:GetChildren() instead? Also you aren’t adding a debounce/cooldown to your Touched event at all, which is firing a hecc of a lot times which could be resulting in that error

1 Like

Yeah, there is a :GetChildren() in the actual code. There is a cooldown, theres chunkTouching[chunk] which will be false if theres no player and true if there is and it updates every 5 seconds

You should probably implement another wait() statement inside the touched function before you end) it just to be sure that it couldn’t be the issue :thinking:

2 Likes

hmm alright that worked, but there’s still a intense frame rate drop whenever i approach 3-4 enemies chunks, do you know why?

It’s lagging for no apparent reason,

It might be because since you’re using GetTouchingParts() so often but idk, we could potentially break this down into this specific code here:

local entity = game.ServerStorage.Entities:FindFirstChild(v.Name):Clone()
entity.HumanoidRootPart.CFrame = v.CFrame + Vector3.new(0,5,0)
entity.Parent = game.Workspace.Entities
Spawns[chunk] = entity
setupEntity(entity,v)
wait(5)

You can try adding breakpoints to confirm where the issue may occur before that line of code gets activated

2 Likes