Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[SOLVED] 'while true do' statement behaves strangely?

Asked by 4 years ago
Edited 4 years ago

Edit: The script wasn't registering soundInstance.TimeLength, so I had to force it to yield until said property was greater than 0. Now it works just fine!

What I'm trying to make here is a script that starts playing music upon the server starting up. My problem is that while true do seems not to behave properly when starting a solo playtest in Roblox Studio. Here's the full code:

local playlist = {
    "5834656055",
    "3124464124",
    "5217321559"
}

local soundInstance = workspace:FindFirstChild("Music") do
    if not soundInstance then
        soundInstance = Instance.new("Sound", workspace)
        soundInstance.Name = "Music"
    end
end

while true do
    for _, v in pairs(playlist) do
        if typeof(v) ~= "string" then
            continue
        end
        print(v)
        soundInstance.SoundId = "rbxassetid://" .. v
        soundInstance:Play()
        wait(soundInstance.TimeLength)
    end
end

Instead of the intended behavior, the script seems to either skip the first element of playlist and play half of the second element, then skip to the third one or play the first element of playlist, skip the second one and play the third. And yes, I've tested the integrity of each audio ID. Why does this happen? Is there any workaround to this? I'm not new to Lua, but I've never had this happen before.

0
Debounce although that probab;y wouldn't work, and try doing a while wait() loop A_Mp5 222 — 4y
0
Forgot to update this: the script wasn't registering the length of the sound, so I had to force it to yield until soundInstance.TimeLength was greater than 0. SimplyConst 0 — 4y

Answer this question