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
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
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
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
That can work too though.