Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make npc not clone multiple times?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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

01local canSpawn = false
02local debounce = false
03 
04function loadNPC()
05    if canSpawn then
06        local Monster = game.ReplicatedStorage.NPC:Clone()
07        Monster.Parent = workspace
08    end
09end
10 
11game.Lighting.Changed:Connect(function()
12    if game.Lighting:GetMinutesAfterMidnight() == 1200 then -- better way of checking time
13        if not debounce then
14            debounce = true
15 
View all 23 lines...

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

Ad
Log in to vote
0
Answered by
MpAlex 16
4 years ago

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

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

or like this

01local debounce = 1
02if game.Lighting.TimeOfDay == "20:00:00" then
03    if debounce == 1 then
04    while true do
05        local Monster = game.ReplicatedStorage.NPC:Clone()
06        Monster.Parent = workspace
07        Wait(5)
08    debounce = 0
09    end
10    end
11end
0
That can work too though. rayhansatria 142 — 4y

Answer this question