Is there a reason this would lag?

oh ok, well if we could see how many frames were dropping that would help

you can probably get one for free easily

Is there one in dev console? :frowning:

I don’t know, I don’t use the dev console much at all

Put this code inside a textlabel, it should show your frames.

local FpsLabel = script.Parent
local Heartbeat = game:GetService("RunService").Heartbeat
local tick = tick

local LastIteration, Start
local FrameUpdateTable = { }

local function HeartbeatUpdate()
	LastIteration = tick()
	for Index = #FrameUpdateTable, 1, -1 do
		FrameUpdateTable[Index + 1] = (FrameUpdateTable[Index] >= LastIteration - 1) and FrameUpdateTable[Index] or nil
	end

	FrameUpdateTable[1] = LastIteration
	local CurrentFPS = (tick() - Start >= 1 and #FrameUpdateTable) or (#FrameUpdateTable / (tick() - Start))
	CurrentFPS = CurrentFPS - CurrentFPS % 1
	FpsLabel.Text = "FPS: " .. CurrentFPS
end

Start = tick()
Heartbeat:Connect(HeartbeatUpdate)

Go to File (at the top left corner) > Studio Settings > Studio and toggle “Show Diagnostics Bar.” That should show you the FPS in the bottom right corner.

oh I never knew this was a feature, I just use a fps counter from my recording software so I guess I never realized

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().
		
	end

end

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 + Vector3.new(0, 10, 0)
end

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

ZombieFolder.ChildRemoved:Connect(checkForZombie)
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!

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

if not game.Workspace.ZombieFolder.Zombie2 then
...
end

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.