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 3 years ago
Edited 3 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 3 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.

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

Ad
Log in to vote
0
Answered by
MpAlex 16
3 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

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
0
That can work too though. rayhansatria 142 — 3y

Answer this question