Is there a reason this would lag?

The average is 60.1 but when it freezes up it goes down to 16

Could I get a little more context on what the use of the script is? That would help out quite a bit.

It spawns a zombie but also makes sure that there is not a another zombie in the game.

This seems like a strange way to approach this concept. You should not need to rename your zombie model in order to check if it exists- in fact it seems counterintuitive if only one is allowed to exist at a time. You should also always try and use :Wait() over wait() for indefinite yields. See if this code works any better. You may need to make some changes.

local ServerStorage = game:GetService("ServerStorage")
local Workspace = game:GetService("Workspace")

local YieldTimer = 6

while true do
	if not Workspace:FindFirstChild("Zombie") then
		local NewZombie = ServerStorage.Zombie:Clone()
		NewZombie.Parent = Workspace -- You can change this to wherever you need to.
		repeat NewZombie.AncestryChanged:Wait() until not NewZombie:IsDescendantOf(Workspace)
		wait(YieldTimer) -- I recommend moving to a custom yield function instead of using wait().


Still lags. :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning:

There is no reason it would lag with the code I provided- something else is happening inside of your game and outside of this script.

they said it’s the script and the lag stops when it is disabled

you are probably right but idk what they did

This code works fine because I have tested it. The problem exists elsewhere.

that’s why I said you are probably right

if this still applies to your script then I’m confused

1 Like

I think I found a somewhat different approach that should work better.

local ServerStorage = game:GetService("ServerStorage")

local ZombieFolder = workspace:WaitForChild("ZombieFolder")
local Zombie = ServerStorage:WaitForChild("Zombie")
local SpawnPart = script.Parent

local function createZombie()
	local newZombie = Zombie:Clone()
	newZombie.Parent = ZombieFolder
	newZombie.PrimaryPart.CFrame = SpawnPart.CFrame +, 10, 0)

local function checkForZombie(instance)
	if instance.Name == "Zombie" then

createZombie() -- you can remove this if the zombie is already in the Workspace

Your code should be already lag-free, but check if this is any better.

It could be because the zombie I am cloning has a lot of scripts.

1 Like

hmm if you’re planning on cloning a lot of zombies and then it’s starting to lag a lot then there might be several reasons behind it.

  1. Each zombie has a .Touched function and it initiate whenever they touch something
    Solution: Use magnitude detection instead or raycast since they’re a whole lot cheapter in terms of performances.

  2. A lot of loops running a lot of thing at the same time.
    Solution: All of the zombies should be ran in a single loop instead of a whole lot of loops in each and every single zombies.

  3. Animation lags (Bunch of server side animation script such as idle, jump etc)
    Solution: I’m not sure if this is possible but it’d be the best to run the animations code through local script.

  4. Not disconnecting loops properly
    Solution: Make sure you disconnect all of the loops properly!, while this maybe impossible because Roblox literally disconnect them for you whenever the script is removed from the game, it is still possible that you might forget to disconnect it.

  5. wait lag
    Solution: Most people said that wait() isn’t the best choice when it comes to utilising it with a lot of stuff at the same time, I suggest you use Heartbeat:Wait() instead for a better performances, more info about it here!

I hope these helps!

Is there any reason of using :FindFirstChild() and not the . specifier like:

if not game.Workspace.ZombieFolder.Zombie2 then

It’s confirmed that it takes 20% longer using :FindFirstChild() rather than the . specifier.

This code will error every time that Zombie2 does not exist (meaning it will never continue because the statement only proceeds if Zombie2 does not exist).

There is nothing inherently laggy about the code that is being used; so, the problem lies outside of this script.

Do you know any zombie models that do NOT attack other zombies and don’t have a ton of scripts? The problem might be that the zombie has a lot of scripts.

I would recommend coding your own zombies. Perhaps you want pathfinding. That would depend on how many zombies there are at once. It also depends on how you want the zombies to operate. Is it a round based game like Black Ops Zombies? Is it an open world zombie survival game where zombies spawn somewhat randomly?

It is like an extra minigame

s in a tycoon. I have no idea how to script a zombie

Also I am using the tall lifelike zombie from roblox.

Still lags. :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning:

Dang, I think it’s the zombie then, I’m all out of ideas.