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

What is the problem with my playMusic script? [RESOLVED]

Asked by 9 years ago

It works in Play Solo but it does not work when you actually play the game. What is the problem?

function playMusic()
    game.Players:WaitForChild("Player")
    if game.Players:findFirstChild("Player") ~= nil then
        game.Workspace.Music:Play()     
    end
end
repeat wait(0.1) until game.Workspace.Music ~= nil
playMusic()

It is a local script, by the way.

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

The player is only named 'Player' when you're on PlaySolo. Any other time it's the same name as the person's username. Your script will therefore eternally wait on line 02, since there will never be someone named 'Player' in the actual game.

If you want the music to play whenever the game starts, then you don't need anything nearly as complicated. Just put this code inside the sound:

script.Parent:Play()

It will make the sound play as soon as the game starts.

0
I know, but for some people the sound asset doesn't load if they lag. Thank you anyway though. dirty_catheter 7 — 9y
0
You can search preloading on the wiki and learn to prevent that. Perci1 4988 — 9y
Ad

Answer this question