Zombie Spawn script broken, please help

For some reason, my zombie spawn script is spawning tens of zombies a second, forming a giant pile. The day/night cycle in the script is constantly switching from day to night every half a second or so. I would also like to know how i could potentially use multiple spawn locations. My code is below.

local Lighting = game:GetService("Lighting")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Set the duration of each day and night cycle (in seconds)
local dayDuration = 60
local nightDuration = 30

-- Set the time when night starts (in hours, 24-hour format)
local nightStartHour = 20
local nightEndHour = 8

-- Set the zombie spawn location
local zombieSpawnLocation = Vector3.new(-63.078, 0.5, 631.332) 

-- Load the zombie model from ReplicatedStorage
local zombieModel = ReplicatedStorage:WaitForChild("Zombie"):Clone()

-- Function to spawn zombies
local function spawnZombies()
	local zombie = zombieModel:Clone()
	zombie.Parent = workspace
	zombie:SetPrimaryPartCFrame(CFrame.new(zombieSpawnLocation))
end

-- Function to switch between day and night
local function switchDayNight()
	if Lighting.ClockTime >= nightStartHour or Lighting.ClockTime < nightEndHour then
		-- It's night, spawn zombies
		spawnZombies()
	end

	-- Switch between day and night
	if Lighting.ClockTime >= nightStartHour then
		-- It's night, switch to day
		Lighting.ClockTime = 6 -- Change this to your desired day start time
	else
		-- It's day, switch to night
		Lighting.ClockTime = nightStartHour -- Change this to your desired night start time
	end
end

-- Function to update the time
local function updateTime(deltaTime)
	Lighting.ClockTime = Lighting.ClockTime + deltaTime
end

-- Connect the functions to the appropriate events
game:GetService("RunService").Heartbeat:Connect(updateTime)
game:GetService("RunService").Heartbeat:Connect(switchDayNight)
3 Likes

Every heartbeat (which from what I remember is every time your device renders the next frame triggers the event) you are spawning a zombie

What you could do is add an extra check to ensure that zombies don’t spawn every heartbeat


local SpawnDelta = os.clock()
local SpawnCooldown = 10

if Lighting.ClockTime >= nightStartHour or Lighting.ClockTime < nightEndHour then
       --It's night, spawn zombies
       if os.clock() - SpawnDelta > SpawnCooldown then
              SpawnDelta = os.clock()
              spawnZombies()
       end
end
```

I implemented this change, however the day/night cycles is switching even faster than before, so fast that the zombies can’t even spawn.

lightning’s clock time property goes from 0 to 24 and you are adding the interval between each heartbeat which, even though it is a very low value, is added several times

What I would recommend you do is divide the delta time by 10 which would make the time update slower

local function updateTime(deltaTime)
	Lighting.ClockTime = Lighting.ClockTime + deltaTime / 10
end

I added this but it’s still flashing like mad. I’m very confused. Could you look over my code and see if there are any obvious errors? Also, I set it to notify me in the console whenever it updates, and it updated 163 times in 5 seconds…

local Lighting = game:GetService("Lighting")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnDelta = os.clock()
local SpawnCooldown = 10


-- Set the duration of each day and night cycle (in seconds)
local dayDuration = 60
local nightDuration = 30

-- Set the time when night starts (in hours, 24-hour format)
local nightStartHour = 20
local nightEndHour = 6

-- Set the zombie spawn location
local zombieSpawnLocation = Vector3.new(-63.078, 0.5, 631.332)

-- Load the zombie model from ReplicatedStorage
local zombieModel = ReplicatedStorage:WaitForChild("Zombie"):Clone()

-- Function to spawn zombies
local function spawnZombies()
	local zombie = zombieModel:Clone()
	zombie.Parent = workspace
	zombie:SetPrimaryPartCFrame(CFrame.new(zombieSpawnLocation))
end

-- Function to switch between day and night
local function switchDayNight()
	if Lighting.ClockTime >= nightStartHour or Lighting.ClockTime < nightEndHour then
		print("time updated")
		-- It's night, spawn zombies
		if os.clock() - SpawnDelta > SpawnCooldown then
			SpawnDelta = os.clock()
			spawnZombies()
		end
	end

	-- Switch between day and night
	if Lighting.ClockTime >= nightStartHour then
		-- It's night, switch to day
		Lighting.ClockTime = 6 -- Change this to your desired day start time
	else
		-- It's day, switch to night
		Lighting.ClockTime = nightStartHour -- Change this to your desired night start time
	end
end

-- Function to update the time
local function updateTime(deltaTime)
	Lighting.ClockTime = Lighting.ClockTime + deltaTime / 10
end

-- Connect the functions to the appropriate events
game:GetService("RunService").Heartbeat:Connect(updateTime)
game:GetService("RunService").Heartbeat:Connect(switchDayNight)

Can you send a video of what this looks like in the studio?


eDIT: I forgot to stop recording so it also shows me trying to upload it to youtube :skull:

1 Like

If you remove these 2 checks you can get the hours to pass normally again

local function switchDayNight()
	if Lighting.ClockTime >= nightStartHour or Lighting.ClockTime < nightEndHour then
		print("time updated")
		-- It's night, spawn zombies
		if os.clock() - SpawnDelta > SpawnCooldown then
			SpawnDelta = os.clock()
			spawnZombies()
		end
	end

	-- Switch between day and night
	--[[if Lighting.ClockTime >= nightStartHour then
		-- It's night, switch to day
		Lighting.ClockTime = 6 -- Change this to your desired day start time
	else
		-- It's day, switch to night
		Lighting.ClockTime = nightStartHour -- Change this to your desired night start time
	end]]
end

the time is passing normally now, but the zombies are not spawning.

keeps printing “time updated” ?

Yeah, it does. I’m not entirely sure what’s causing the zombies to not spawn because there aren’t any errors.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnDelta = os.clock()
local SpawnCooldown = 10


-- Set the duration of each day and night cycle (in seconds)
local dayDuration = 60
local nightDuration = 30

-- Set the time when night starts (in hours, 24-hour format)
local nightStartHour = 20
local nightEndHour = 6

-- Set the zombie spawn location
local zombieSpawnLocation = Vector3.new(-63.078, 0.5, 631.332)

-- Load the zombie model from ReplicatedStorage
local zombieModel = ReplicatedStorage:WaitForChild("Zombie"):Clone()

-- Function to spawn zombies
local function spawnZombies()
	local zombie = zombieModel:Clone()
	zombie.Parent = workspace
	zombie:SetPrimaryPartCFrame(CFrame.new(zombieSpawnLocation))
end

-- Function to switch between day and night
local function switchDayNight()
	if Lighting.ClockTime >= nightStartHour or Lighting.ClockTime < nightEndHour then
		print("time updated")
		-- It's night, spawn zombies
		if os.clock() - SpawnDelta > SpawnCooldown then
			SpawnDelta = os.clock()
			spawnZombies()
		end
	end
end

-- Function to update the time
local function updateTime(deltaTime)
	Lighting.ClockTime = Lighting.ClockTime + deltaTime / 10
end

-- Connect the functions to the appropriate events
game:GetService("RunService").Heartbeat:Connect(updateTime)
game:GetService("RunService").Heartbeat:Connect(switchDayNight)```

Could you put a print inside the spawnzombie function because I’m not sure if the function is being executed

Yes. Also I left the game running for a moment and a few zombies spawned (around 3) but I’m not sure how because from what I could tell the function wasn’t fired.

Edit: turns out it was fired but only two times during a very specific part of the night.
I’m also repeatedly getting this error:
Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this.
I’m also thinking about replacing Model:SetPrimaryPartCFrame() with Model.Position to see if that fixes anything

well the zombie spawn time is quite high you could try changing it to something shorter

local SpawnCooldown = 2

Regarding the error that is occurring, it can happen when the spawned model was destroyed or when you did not define the primary part

local function spawnZombies()
	local zombie = zombieModel:Clone()
	zombie.Parent = workspace
    zombie.PrimaryPart = zombie:WaitForChild("HumanoidRootPart")

zombie:SetPrimaryPartCFrame(CFrame.new(zombieSpawnLocation))
end

I think the issue is that I didn’t define it. But it doesn’t seem to break anything so I’m not gonna mess with it.

well the zombie spawn time is quite high you could try changing it to something shorter

this fixed it! thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.