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)
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
```
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)
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
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)```
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
local function spawnZombies()
local zombie = zombieModel:Clone()
zombie.Parent = workspace
zombie.PrimaryPart = zombie:WaitForChild("HumanoidRootPart")
zombie:SetPrimaryPartCFrame(CFrame.new(zombieSpawnLocation))
end