Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

no functions seem to be running anyone know why?

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

2 comments

0

Because you have a loop and until that loop ends you cannot do anything...

0

its working fine now, thanks! (i have learned something very important: loops that never end should come last)

1 answer

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

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.