Will this NPC spawner script work forever?

So I made this NPC spawner, that only spawns the NPCs at night, I wanted to test if it lasts forever, but I don’t want to wait 24 minutes just to test if this script worked properly. This script already works, but I just wanted you guys to review this script and see if it will work forever, not just once.

IF THIS IS THE WRONG CATERGORY, PLEASE TELL ME.


Server script inside of ServerScriptService.

local lighting = game:GetService("Lighting")
local db = false

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if lighting:GetMinutesAfterMidnight() > 6 then
		print("Day")
		for i,v in pairs(workspace.NightTimeNpcs:GetDescendants()) do
			if v ~= nil then
				if v:IsA("Humanoid") then
					v.Health = 0
					wait(2)
					if v.Parent ~= nil then
						v.Parent:Destroy()
					end	
					wait(1)
				end
			end
		end
	elseif lighting:GetMinutesAfterMidnight() < 6 then
		print("Night")
		spawn(function()
			for i = 1,5 do
				if not db then
					db = true
					local Part = game.Workspace.GeneratePart

					local NPCs = game.ServerStorage.NightTimeNpcs:GetChildren()

					local Speed = NPCs[math.random(1,#NPCs)]:Clone()

					Speed.HumanoidRootPart.CFrame = Part.CFrame * CFrame.new(math.random(-Part.Size.X/2, Part.Size.X/2),math.random(-Part.Size.Y/2, Part.Size.Y/2),math.random(-Part.Size.Z/2, Part.Size.Z/2))
					Speed.Parent = workspace.NightTimeNpcs
					wait(5)
					if lighting:GetMinutesAfterMidnight() > 6 then
						break
					end
					db = false
				end
				
			end
		end)	
	end
end)

Day and night cycle in workspace.

local dayLength = 12

local cycleTime = dayLength*60
local minutesInADay = 24*60

local lighting = game:GetService("Lighting")

local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime

local timeRatio = minutesInADay / cycleTime

if dayLength == 0 then
	dayLength = 1
end

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(1/15)
until false

RBXM FILE:

rbxm file thing.rbxm (10.2 KB)

3 Likes

I have read the code and It looks like it should work, I don’t see any errors that would cause it not to work. However you could make few improvement to the scripts , such as using ipairs on array, using coroutine instead of spawn.

4 Likes