A little bit confused with while loops

yes, I’m an experienced scripter and I still ask basic scripting questions but here I go

I heard that if your code is similar to this:

while true do
    print("lol")
end

print("Hey")

The code only keep spamming the lol word, and it will never continue to the rest of the code. Am I missing something? Because if I’m trying to do a retry on getting a player’s DataStore in one thread/script, it would look something like this:

local count = 0
local success, result

success, result = pcall(function()
    return DataStore:GetAsync(player.UserId)
end)

if success then
    — load data
else
    print("DataStore error: "..result)
    while not success do
        if count <= 5 then
            print("Retrying...")
            wait(10)
            break
        success, result = pcall(function()
            return DataStore:GetAsync(player.UserId)
        end)
        count += 1
    end
end

Am I doing something wrong? Or am I missing something?

ALSO, ignore the fact that while true loops will cause exhaustion, just imagine while true loops wouldn’t cause this error in this case.

Your if statement is missing an end. Furthermore, after if the call errored, the script will wait for 10 seconds and just break the loop. It will not reach the block of code where you call the DataStore function again.

Thank you for reminding me, but that’s only for an example, I want to know the thing about the whole true loop I asked.

I don’t really understand your loop question. But if you’re asking if “lol” will keep printing forever then you are correct.

But, will “Hey” be printed as well?

It won’t be printed. The while loop may only be broken if the condition is false. The condition is true always in this case so the while loop will go on forever without reaching “Hey.”

1 Like

I think this will conclude the solution. Thanks.