How could I make this script not lag & spawn randomly?

For some reason, this script I put together still lags and doesn’t spawn random NPC’s (It seems to select a certain name and continue to spawn that npc after the other npc died), the main problem I have is the NPC’s not spawning randomly (Explained above) and weapons lagging (sometimes) when more spawn.

local Spawns = workspace:WaitForChild("World").Objects:WaitForChild("Map").Locations:GetChildren()
local NPC = replicatedStorage:WaitForChild("Assets").Zombies:GetChildren()
local rand = math.random(1,#NPC)

if game.ReplicatedStorage.Match.Value == true then
	local children = workspace:WaitForChild("World").Objects:WaitForChild("Map").Zombies:GetChildren()
	local loco = game.Workspace:WaitForChild("World").Objects:WaitForChild("Map").Zombies
	local count = #children
	
	loco.ChildRemoved:Connect(function(child)
		wait(0.1)
	local NPCs = NPC[rand]:Clone()
	NPCs:PivotTo(Spawns[math.random(#Spawns)].CFrame)
	NPCs.Parent = loco
	end)
	
else
	print("Cannot spawn, match isn't ready")
end

Fix for random NPC spawn:

local Spawns = workspace:WaitForChild("World").Objects:WaitForChild("Map").Locations:GetChildren()
local NPC = replicatedStorage:WaitForChild("Assets").Zombies:GetChildren()
local lastSpawnedNPC = nil

local function getRandomNPC()
     local randNPC = NPC[math.random(1,#NPC)]
     if randNPC == lastSpawnedNPC then
         return getRandomNPC()
     else
         lastSpawnedNPC = randNPC
         return randNPC
     end
end)

if game.ReplicatedStorage.Match.Value == true then
	local children = workspace:WaitForChild("World").Objects:WaitForChild("Map").Zombies:GetChildren()
	local loco = game.Workspace:WaitForChild("World").Objects:WaitForChild("Map").Zombies
	local count = #children
	
	loco.ChildRemoved:Connect(function(child)
		wait(0.1)
	local NPCs = getRandomNPC():Clone()
	NPCs:PivotTo(Spawns[math.random(#Spawns)].CFrame)
	NPCs.Parent = loco
	end)
	
else
	print("Cannot spawn, match isn't ready")
end
1 Like

thank you, got any idea how I could trouble shoot the lag?

its basically where the server has to much memory being used and causes weapons to lag visually

1 Like

One thing you could do is check the Script Performance in the View tab on Studio.

Alright, it does take a while for the lag to occur so it shouldnt be too big, just on release it needs to be fixed but i will check out the performance stats

1 Like

One last thing, Is there a way I can set how many NPC’s i want to spawn within that script? If I duplicate the child.removed it does a x2 type thing.

local amountOfNPC = 5 --// amount of NPCs you want to spawn

local function getRandomNPC()
     local randNPC = NPC[math.random(1,#NPC)]
     if randNPC == lastSpawnedNPC then
         return getRandomNPC()
     else
         lastSpawnedNPC = randNPC
         return randNPC
     end
end)

for i = 1, amountOfNPC do
    local NPCs = getRandomNPC():Clone()
	NPCs:PivotTo(Spawns[math.random(#Spawns)].CFrame)
	NPCs.Parent = loco
    task.wait()
end
2 Likes