#Looops getting added when called in a function

1 messages · Page 1 of 1 (latest)

prime lark
#

i have set up a for i= 1... loop in a function, so that the loop is beeing called. Somehow after the loop is beeing broken bcc of a intended other factor, the next time it get's called 2 loops are beeing played.

this is the code:
ReplicatedStorage.wait.Event:Connect(function()
for i = 1, disasterTime do
wait(1)
print(i)

    if i == disasterTime and fix == false then 
        Skip = true 
    end
        if fix == true then 

fix = false
break
end
end
end)

woven holly
#

Try replacing it with return mabey🙏

prime lark
#

ok i will try it thanks for the tip already : D

#

i still encounter the same problem

somber totem
#

,\

#

.

#

.

#

.

#

.

#

.

#

.

weak marten
# prime lark i still encounter the same problem

ReplicatedStorage.wait.Event:Connect(function()
for i = 1, disasterTime do
task.wait(1) -- wait 1 second (use task.wait instead of wait because devs cried about it)

    print(i)

    if fix then
        -- If someone fixes the disaster mid-countdown, stop the loop early.
        fix = false
        break
    end

    if i == disasterTime and not fix then
        -- Time ran out and nobody fixed it.
        Skip = true
    end
end

end)

prime lark
#

thanks for the help, ima try it out and look if it works properly : D

weak marten
#

whats the problem now

prime lark
#

the problem is that is still prints two times the i value so it means that 2 loops are beeing run if i am not mistaken

prime lark
#

ok

weak marten
#

If i is printing twice, you’re probably not crazy—your event is getting connected multiple times.

The problem isn’t the loop itself,
it’s that you’re probably doing:
Modifier
ReplicatedStorage.wait.Event:Connect(function()
-- loop here
end)
multiple times, so you have 2 or more connections stacked.

prime lark
#

yeah youre right

weak marten
#

If you only want one listener at a time , store the connection and disconnect it before re-adding:

#

local waitEventConnection

-- Before connecting again:
if waitEventConnection then
waitEventConnection:Disconnect()
end

waitEventConnection = ReplicatedStorage.wait.Event:Connect(function()
-- your loop here
end)

prime lark
#

thanks 😄

#

let me try it

#

maybe i just understood it wrong it still doesn't work

#

local waitEventConnection

-- Before connecting again:
if waitEventConnection then
    waitEventConnection:Disconnect()
end

waitEventConnection = ReplicatedStorage.wait.Event:Connect(function()
for i = 1, disasterTime do
    task.wait(1)  -- wait 1 second (use task.wait instead of wait because devs cried about it)

    print(i)

    if fix then
        -- If someone fixes the disaster mid-countdown, stop the loop early.
        fix = false
        break
    end

    if i == disasterTime and not fix then
        -- Time ran out and nobody fixed it.
        Skip = true
    end
end
end)
#

that's what it looks rn

weak marten
#

what is it spittin rn in log

prime lark
#

wait i think i just messed up

timid quartzBOT
#

studio** You are now Level 3! **studio

prime lark
#

i just pasted your code and then deleted the old one

weak marten
#

look in the console

#

and copy paste the console

prime lark
#

ye

#

19:32:07.149 Stack End - Studio
19:32:20.281 1 - Server - elevatorsciptv5:129
19:32:21.297 2 - Server - elevatorsciptv5:129
19:32:22.299 3 - Server - elevatorsciptv5:129
19:32:23.313 4 - Server - elevatorsciptv5:129
19:32:24.314 5 - Server - elevatorsciptv5:129
19:32:25.330 6 - Server - elevatorsciptv5:129
19:32:26.348 7 - Server - elevatorsciptv5:129
19:32:27.363 8 - Server - elevatorsciptv5:129
19:32:28.364 9 - Server - elevatorsciptv5:129
19:32:29.364 10 - Server - elevatorsciptv5:129
19:32:30.380 11 - Server - elevatorsciptv5:129
19:32:31.381 12 - Server - elevatorsciptv5:129
19:32:32.397 13 - Server - elevatorsciptv5:129

#

19:32:33.413 14 - Server - elevatorsciptv5:129
19:32:34.414 15 - Server - elevatorsciptv5:129
19:32:35.430 16 - Server - elevatorsciptv5:129
19:32:55.296 1 - Server - elevatorsciptv5:129
19:32:55.296 1 - Server - elevatorsciptv5:129
19:32:56.313 2 - Server - elevatorsciptv5:129
19:32:56.314 2 - Server - elevatorsciptv5:129
19:32:57.329 3 - Server - elevatorsciptv5:129
19:32:57.330 3 - Server - elevatorsciptv5:129
19:32:58.346 4 - Server - elevatorsciptv5:129
19:32:58.346 4 - Server - elevatorsciptv5:129
19:32:59.362 5 - Server - elevatorsciptv5:129
19:32:59.362 5 - Server - elevatorsciptv5:129
19:33:00.379 6 - Server - elevatorsciptv5:129
19:33:00.379 6 - Server - elevatorsciptv5:129
19:33:01.395 7 - Server - elevatorsciptv5:129
19:33:01.395 7 - Server - elevatorsciptv5:129
19:33:02.396 8 - Server - elevatorsciptv5:129
19:33:02.396 8 - Server - elevatorsciptv5:129
19:33:03.412 9 - Server - elevatorsciptv5:129
19:33:03.412 9 - Server - elevatorsciptv5:129
19:33:04.430 10 - Server - elevatorsciptv5:129
19:33:04.430 10 - Server - elevatorsciptv5:129
19:33:05.446 11 - Server - elevatorsciptv5:129
19:33:05.446 11 - Server - elevatorsciptv5:129
19:33:06.462 12 - Server - elevatorsciptv5:129
19:33:06.462 12 - Server - elevatorsciptv5:129
19:33:07.479 13 - Server - elevatorsciptv5:129
19:33:07.479 13 - Server - elevatorsciptv5:129
19:33:08.495 14 - Server - elevatorsciptv5:129
19:33:08.495 14 - Server - elevatorsciptv5:129
19:33:09.512 15 - Server - elevatorsciptv5:129
19:33:10.529 16 - Server - elevatorsciptv5:129
19:33:11.546 17 - Server - elevatorsciptv5:129

#

19:33:12.562 18 - Server - elevatorsciptv5:129
19:33:13.562 19 - Server - elevatorsciptv5:129
19:33:14.578 20 - Server - elevatorsciptv5:129
19:33:15.596 21 - Server - elevatorsciptv5:129
19:33:16.535 Disconnect from ::ffff:127.0.0.1|58694 - Studio
19:33:16.612 22 - Server - elevatorsciptv5:129
19:33:17.628 23 - Server - elevatorsciptv5:129

#

19:33:12.562 18 - Server - elevatorsciptv5:129
19:33:13.562 19 - Server - elevatorsciptv5:129
19:33:14.578 20 - Server - elevatorsciptv5:129
19:33:15.596 21 - Server - elevatorsciptv5:129
19:33:16.535 Disconnect from ::ffff:127.0.0.1|58694 - Studio
19:33:16.612 22 - Server - elevatorsciptv5:129
19:33:17.628 23 - Server - elevatorsciptv5:129

weak marten
#

well

#

what is this script tryna do

#

i got no context

prime lark
#

oh well

#

the whole script adds a stage and the part i showed u is responsible for the time the stage is playable after the time has ran out or a event has been fired, the stage will bve removed

weak marten
#

im out, js ask chatgbt

prime lark
#

it's ok thanks

#

i think i ahve found a solution with your help 🙂

#

ima try it out

#

@weak marten thanks for your help : D because of your help i found a way

#

to fix it

#

thanks a lot