Hmm… is there a way to reset the function? I’m making it for a round system, where it stops when a condition is met, and then resets after a while.
you could have a check before each thing the looped function does instead then
Could you provide an example? I’m not sure what to do.
local ended = nil
start.OnServerEvent:Connect(function()
while true do
if not ended then
-- do something
end
if not ended then
-- do something else
end
wait(1)
end
end)
res:GetPropertyChangedSignal("Value"):Connect(function()
if res.Value ~= nil or res.Value ~= "" then
ended = true
-- I want the while loop above to instantly stop here
end
end)
Wouldn’t that still have a delay, since the while loop checks every second?
it checks ended whenever it does something in this case, even if ended == true then it’s just going to do nothing.
you wouldn’t be stopping inside the loop
you would be stopping outside it
While true loops will continue until you stop it or if it reaches a certain value. For example:
local secondsElapsed = 0 -- Live seconds
local timeout = 5 -- Seconds needed to reach
while true do
print("Looping...")
wait(1)
secondsElapsed = secondsElapsed + 1 -- This value will increase by 1 until it reaches the specific value
if secondsElapsed == timeout then -- if it reaches the value, it ends and continues down the code
break
end
end
print("Loop ended; moving on!") -- Tests to make sure it passes the loop
Reference: Loops - Roblox
that won’t work, they want the code to check all the time, not every 1 second
also you should use +=
start.OnServerEvent:Connect(function()
while true do
-- the rest of my code
wait(1)
end
res:GetPropertyChangedSignal("Value"):Connect(function()
if res.Value ~= nil or res.Value ~= "" then
ended = true
break;
end
end)
end)
Maybe?
I don’t wanna be that guy lol, but i made a module that addresses this very issue with wait()
's lack of flexibility.
Source for the module: CallScheduler.lua (652 Bytes)
Usage in your case:
local scheduler = require(PathToModule)
local function ServerLoop()
scheduler.Add(1, ServerLoop) -- add server loop to call again in 1 second from now
-- the rest of your code
end
start.OnServerEvent:Connect(function()
scheduler.Add(1, ServerLoop) -- start it up
end)
res:GetPropertyChangedSignal("Value"):Connect(function()
if res.Value and res.Value ~= "" then
scheduler.Remove(ServerLoop)
end
end)
This should achieve what you want, but note that is not a good idea to start chains like while loops everytime single time a server event fires, you can crash the server that way.
I don’t think we need a whole module for this to work
besides just use a custom wait to replace wait()
no that wouldn’t work, you aren’t really breaking the loop
I didn’t say you or anyone else needed to use it, i’m just putting it there in case this problem gets on anyone’s nerves.
And also custom wait’s are very non ideal lol, implementing timed thread resumation in lua when it exists in C is quite impractical, but this module happens to apply a wholely different concept (dm me if you want more info on that so we don’t pollute the thread)
dude you’re fine I was just saying that, I never said that you said they needed to use it
it depends on what you define as a custom wait
I define it as a custom made way to yield something
I have a quick unrelated question - how can you comminucate from server script to server script
use bindable events for that
you can also use them to do client to client
Woah nice that’s actually super useful!
Does it make coroutines obsolete?
Mind if I yoink it?
Does it make coroutines obsolete?
It makes the pattern of relying on coroutines just to do something a few seconds from now obsolete, yes. And you can use it all you want if you’d like.
If you have any questions about it dm me, preferably don’t reply