#Weird Loop

1 messages · Page 1 of 1 (latest)

fast mango
#

Alright there HAS to be a more simple way of doing this

    while wait(1) do
        local Playable = true
        
        if CarAlarmCooldown.Value == 0 then
            for i = 1, Interval * 60 do
                if CarAlarmCooldown.Value ~= 0 then Playable = false end
                wait(1)
            end
            if Playable then
                Alarm:Play()
            end
        end
    end
end)```
#

The script works how i cant to but i swear theres gotta be an easier way to do this

wraith field
#
task.spawn(function()
    while true do
        if CarAlarmCooldown.Value == 0 then
            local t = tick()
            repeat task.wait(1) until CarAlarmCooldown.Value ~= 0 or tick() - t >= Interval * 60
            if CarAlarmCooldown.Value == 0 then Alarm:Play() end
        end
        task.wait(1)
    end
end)```
solemn dagger
solemn dagger
fast mango
#

so basically every once in a while an alarm will go off but people can pay to make it so it doesn’t go off,

#

and doing wait(interval*60) will still have the alarm go off once that wait is done even if CarAlarmCooldown.Value == 0

solemn dagger
#

Why is the alarm cooldown external and internal?

#

You only need one piece of state

wraith field
long zenith
fast mango
#

yeah that’s it, But the wait can be anywhere from 2-5minutes

#

and i’m just making sure that within those 2-5minutes someone doesn’t turn it off

long zenith
#

why is it called CarAlarmCooldown? what's the cooldown for?

#

what values can this be other than 0 or not 0? what is the value meant to represent?

fast mango
#

CarAlarmCooldown is a number value in the Player, is is the amount of seconds the player has payed to keep the alarm off

#

value will start at like 3600 whenever the player pays and will -1 every second

long zenith
fast mango
#

yeah

#

the car alarm is just annoying as hell and i thought it’d be funny to have to make the play pay currency to turn it off

#

for an hour at a time

long zenith
#

why do you compare CarAlarmCooldown to 0? are you changing it every frame?

#

i.e you have a second while loop that does while(true) do cooldown -= 1 ?

fast mango
#

i have the while wait(1) so that once the interval is over and the car alarm plays the interval will start again

#

and eventually play the car alarm again

#

and yes caralarmcooldown is reduced by 1 every second

#

so eventually it’ll hit 0

long zenith
#

state machine is probably better

#

like lua alarmingplrs={} function tripalarm(plr) if alarmingplrs[plr] then return end alarmingplrs[plr]=task.delay(Interval*60,alarm.Play,alarm) end function stopalarm(plr) if not alarmingplrs[plr] then return end pcall(task.cancel,alarmingplrs[plr]) alarmingplrs[plr]=nil if alarm.IsPlaying then alarm:Stop() end end or something

#

then you don't need to reduce cooldown by 1 every second, but i mean if you wanted to do that then you can just add the conditions in getpropertychangedsignal