#Why is there no delay in this loop?

1 messages · Page 1 of 1 (latest)

deft pebble
#

because you're creating multiple while loop everytime it touched

cosmic spoke
#

use task.wait()

#

since wait is deprecated

mental olive
mental olive
mental olive
# deft pebble whats your goal

uhm, well im using print just to get the delay working, but essentially its an enemy system that will maintain 5 enemies even if they are killed

#

fire pfp btw

deft pebble
#

so the touch event is just a starting

#

not mine

mental olive
#

still fire

mental olive
#

i thought touch was just a single detection

deft pebble
#

use clickdetector

#

use ClickDetector, connect to the clicked event, set a variable to true

#

use while loop below that even

#
while task.wait(1) do
  if enabled then
    bla bla
  end
end
#

oh when you entered the zone

#

aight

#

create a variable for enabling/disabling purpose, use touched for setting the variable to true, use touchended to set it to false
create a while loop after those events using the same code i sent above

mental olive
#

mmmm smart

#

ok tysm

deft pebble
#

and stop playing roblox

#

36 hours consecutively

mental olive
#

uhm thats called bee swarm simulator

deft pebble
mental olive
# deft pebble

im very beginner if u couldnt tell already, but I kinda got it to work, except instead of spawning 5, and then waiting to spawn another once one dies, it spawns 3 and then despawns all of them

local enemynum = 0
local storage = script.Parent.Parent.current
local inzone = false

script.Parent.Touched:Connect(function()
    inzone = true
end)

script.Parent.TouchEnded:Connect(function()
    inzone = false
end)

while task.wait(1.5) do
    if inzone == true and enemynum <= 5 then
        local enemy = game.ServerStorage.enemy:Clone()
        enemy.Parent = storage
        enemynum = enemynum+ 1
    elseif inzone == false then
        storage:ClearAllChildren()
    end
end
gray jackal
#

thats because the first mob that gets spawned in triggers the TouchEnded

#

which in turn triggers the storage:ClearAllChildren()

#

are you trying to do something like
if there are 5 mobs in the area than it stops, but if there are 5 mobs and one is outside the area, than it continues spawning?

#

kinda like a minecraft spawner

#

i would recommend instead of inzone being a true or false, you can make it into a list that adds the enemies to it, and if they walk out the zone, you remove them from the list

#

lists come with a count feature too so you dont need the enemynum variable

mental olive