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

Level music script not working, can someone tell me if there is something wrong with it?

I am making a game with levels and each level has its own music. When switching to level 2, the game does not change the music. This script is a LocalScript in StarterPlayerScripts. No error shows up in the output. Also, this is only for two levels.

wait()
local l = game.Players.LocalPlayer.leaderstats.Level.Value

while true do
	wait()
if l == 1 and script.Level1.IsPlaying == false then script.Level1:Play()
	else if l == 2 and script.Level2.IsPlaying == false then script.Level2:Play() script.Level1:Stop()
	end
	end
	end

1 answer

✓ Accepted answer

The problem is the fact that you've defined the value beforehand already, meaning even if the value of the IntValue changes, the variable won't change.

also, a better way to do this would be to use GetPropertyChangedSignal, instead of a while true do loop.

local l = game.Players.LocalPlayer:WaitForChild("leaderstats").Level

l:GetPropertyChangedSignal("Value"):Connect(function()
        if l.Value == 1 then
            script.Level1:Play()
            script.Level2:Stop()


        elseif l.Value == 2 then
            script.Level1:Stop()
            script.Level2:Play()

        end
 end)

1 comment

0

Ohhhhhh... that makes sense, thank you!

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