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

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

Asked by
zGavinz 11
3 years ago

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

Log in to vote
0
Answered by
Subsz 366 Moderation Voter
3 years ago

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)

0
Ohhhhhh... that makes sense, thank you! zGavinz 11 — 3y
Ad

Answer this question