Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make npc not clone multiple times?

Whenever I launch my game it always clones multiple npc and I just want it to clone one.

Sciript: if game.Lighting.TimeOfDay == "20:00:00" then local Monster = game.ReplicatedStorage.NPC:Clone() Monster.Parent = workspace end

2 answers

✓ Accepted answer

The reason why your NPC is spawning more than 1, is because the script keeps checking if the TimeOfDay is 20:00:00.

local canSpawn = false
local debounce = false

function loadNPC()
	if canSpawn then
		local Monster = game.ReplicatedStorage.NPC:Clone()
		Monster.Parent = workspace
	end
end

game.Lighting.Changed:Connect(function()
	if game.Lighting:GetMinutesAfterMidnight() == 1200 then -- better way of checking time
		if not debounce then
			debounce = true

			canSpawn = true
			loadNPC()
			canSpawn = false
			wait(1)
			debounce = false
		end
	end
end)

That should work, I tested it in studio. Please accept the answer if you found this a solution

it will clone until is 20:00:01, you can try adding a loop with a delay between them, i think it will work like that

if game.Lighting.TimeOfDay == "20:00:00" then 
	while true do
		local Monster = game.ReplicatedStorage.NPC:Clone() 
		Monster.Parent = workspace 
		Wait(5)
	end
end

or like this

local debounce = 1
if game.Lighting.TimeOfDay == "20:00:00" then
	if debounce == 1 then 
	while true do
		local Monster = game.ReplicatedStorage.NPC:Clone() 
		Monster.Parent = workspace 
		Wait(5)
	debounce = 0
	end
	end
end

1 comment

0

That can work too though.

This discussion is preserved as it was. New answers, comments, and votes are closed.