Waiting 15 seconds before spawning NPC

Im making a wave defence game and im trying to make the enemies spawn after 15 seconds but wait(15) doesnt work and ive tried so many solutions that havent worked
Screenshot 2024-07-24 121448

can you show the code of when you tried doing wait(15)?

1 Like

uhhhh

image

this code will spawn an enemy every 3 seconds endlessly

1 Like

try this

while true do
   local Clone = NPC:Clone()
   Clone:PivotTo(spawner.CFrame)
   Clone.Parent = workspace
   task.wait(15)
end

i just put wait(15) between local textlabel and while true do

1 Like

okay but that will still spawn enemies endlessly, unless that’s what you’re going for

what im going for is after 15 seconds they will start spawning every 3 seconds

1 Like

ohhhhhh

just put a task.wait(15) before the while loop

If thats the case then your code is wrong you have to wait(15) then set whatever value is equal to true and inside write wait(3)

For example:

local spawnTime =  false

wait(15)
spawnTime = true

while spawnTime == true do
   local Clone = NPC:Clone()
   Clone:PivotTo(spawner.CFrame)
   Clone.Parent = workspace
   wait(3)
end
2 Likes

ummmm ackshully

you should use task.wait instead of wait

additionally, you do not need to check whether spawnTime is true

because by default the value will always be true (This is a horrible explanation)

but for example

local variable = false

while variable do -- equivalent to while true do
end

while not variable do -- or while variable == false/~= true (psychopaths use this method) 
end

If it’s not waiting 15 seconds then try using a print statement to debug and also use task.wait().

I realized after I wrote it I didnt have to check that it was true (Im very tired rn) LOL

I’ve never really used task.wait() so it almost always slips my mind but looking at what it does I guess it does make more sense to use it

all these solutions arent working and nothing came out in the output

idk the difference between task.wait() and wait() either

but people use task.wait() more often

but i think task.wait() just doesn’t throttle or something

1 Like

can you put a print statement inside the while loop

Try this:
If thats the case then your code is wrong you have to wait(15) then set whatever value is equal to true and inside write wait(3)

For example:

local spawnTime =  false

wait(15)
spawnTime = true

while spawnTime do
   local Clone = NPC:Clone()
   Clone.HumanoidRootPart.CFrame = spawner.CFrame -- Move the HumanoidRootPart and not the Torso
   Clone.Parent = workspace
   wait(3)
end

According to this post task.wait() is more accurate as it is 2x faster

Side Note: According to an admin we should use task.wait() instead…

2 Likes

oh wait a second

is this a server script?

if so:

local rStorage = game:GetService("ReplicatedStorage")
local enemiesFolder = rStorage:WaitForChild("Enemies")

local NPCs = {
enemiesFolder.EnemyName,
-- add more if you will ever need to
}

local spawner = script.Parent
-- We are getting rid of the textlabel variable, as you cannot access player GUIs on server side, if you want to edit the text label dynamically, you need to use remote events.
local spawnable = false

task.wait(15)
spawnable = true

while spawnable do
  local clone = NPCs.YourEnemy:Clone() -- i'm being too complicated i know, but if you'll ever be adding more enemies, i suggest having a function that's responsible for enemy RNG
  clone.Parent = workspace
  clone.HumanoidRootPart.CFrame = spawner.CFrame
  task.wait(3)
end

why is it unknown? i labeled it in the table

oh it’s because it’s an array :skull:

okay so

you can either do

local NPCs = {
  SomeName = enemiesFolder.YourEnemy
}

OR

NPCs[1]:Clone()