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

no functions seem to be running anyone know why?

Asked by 6 years ago

i have a script where in the middle of the script there is a function that starts like this:

function play()
    print("playing...")

it never says "playing..."

and i have put a play() at the bottom

full code:

local loop_phase = 1
local playing = false
local id = script.Id

while true do
    wait()
    if playing then
        if loop_phase == 1 then
            print("now playing: "..id.Value..".start")
            script.Library[id.Value].start:Play()
            wait(script.Library[id.Value].start.TimeLength)
            loop_phase = 1
        elseif loop_phase == 2 then
            script.Library[id.Value].loop:Play()
            wait(script.Library[id.Value].loop.TimeLength)
        end
    end
end

function play()
    print("playing...")
    for i=0,50,1 do
        wait(0.01)
        if loop_phase == 1 then
            script.Library[id.Value].start.Volume = script.Library[id.Value].start.Volume - 0.2
        elseif loop_phase == 2 then
            script.Library[id.Value].loop.Volume = script.Library[id.Value].loop.Volume - 0.2
        end
    end
    playing = false
    if loop_phase == 1 then
        script.Library[id.Value].start:Stop()
    elseif loop_phase == 2 then
        script.Library[id.Value].loop:Stop()
    end
    script.Library[id.Value].loop.Volume = 10
    script.Library[id.Value].start.Volume = 10

    loop_phase = 1
    playing = true
end

play()

the local id = script.Id is a string value set to "adventure"

the script will locate: script.Library[id.Value]["start"] at some point which basicly means script.Library.start

and start is a sound in a folder called adventure in the script

no errors, or the "playing..." i seriously need help

0
Because you have a loop and until that loop ends you cannot do anything... greatneil80 2647 — 6y
0
its working fine now, thanks! (i have learned something very important: loops that never end should come last) fanofpixels 718 — 6y

1 answer

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
6 years ago
Edited 6 years ago

Remember that a script is linear and performs tasks sequentially. In your problem, the problem arises because the script will start the infinite while loop and never end. Thus, the script can't progress to the code after it. If you want to fix this, the simplest and easiest way is to simply move the infinite loop after everything else. This would look like this:

local loop_phase = 1
local playing = false
local id = script.Id

function play()
    print("playing...")
    for i=0,50,1 do
        wait(0.01)
        if loop_phase == 1 then
            script.Library[id.Value].start.Volume = script.Library[id.Value].start.Volume - 0.2
        elseif loop_phase == 2 then
            script.Library[id.Value].loop.Volume = script.Library[id.Value].loop.Volume - 0.2
        end
    end
    playing = false
    if loop_phase == 1 then
        script.Library[id.Value].start:Stop()
    elseif loop_phase == 2 then
        script.Library[id.Value].loop:Stop()
    end
    script.Library[id.Value].loop.Volume = 10
    script.Library[id.Value].start.Volume = 10

    loop_phase = 1
    playing = true
end

play()

while true do
    wait()
    if playing then
        if loop_phase == 1 then
            print("now playing: "..id.Value..".start")
            script.Library[id.Value].start:Play()
            wait(script.Library[id.Value].start.TimeLength)
            loop_phase = 1
        elseif loop_phase == 2 then
            script.Library[id.Value].loop:Play()
            wait(script.Library[id.Value].loop.TimeLength)
        end
    end
end
0
Adding to this, if for some reason you can't move the while loop to the end, you could use spawn() or coroutines to run them in a separate thread. mattscy 3725 — 6y
Ad

Answer this question