What's Wrong with this Script?

I’m very new to scripting, and I’m trying to make some menu music for a game. How can I get this script to work? The sound doesn’t play whenever I start the game even though the GUI is ticked to Enabled.

Summary

if game.StarterGui[“Class select”].Enabled == true then

script.Parent.menu[“I Wish I Was in Dixie (Instrumental)”]:Play()

end

1 Like

I don’t think the spaces are the issue since the title is in brackets, but I tried removing the spaces anyways. Doesn’t seem to work. Even replaced the names with single words and still no. Here’s the explorer though.

1 Like

You’re running the check on the gui in the StarterGui service and not the player’s PlayerGui (roblox.com).
The conents within StarterGui are cloned to a player’s PlayerGui upon joining the game/resetting (unless ResetOnSpawn is enabled for a ScreenGui). You’ll want to directly index the gui within the PlayerGui instead.

2 Likes

Or maybe the error is due to the script playing the music immediately and pausing it, so it’s unlikely you will hear any sound.
You can try the Stopped event to have the script wait for the music to play until the end, and then will stop the music.

Also put your sound/music to SoundService, else sound will not play.

local sound = game.SoundService.Sound -- Your sound
local screenGui = game.StarterGui.ScreenGui -- Your Screen/Object gui

if screenGui.Enabled == true then
	sound:Play()
	sound.Stopped:Wait()
	sound:Stop()
end
4 Likes

For my original script, why does the music just automatically stop if you haven’t defined a Stopped event?

Since the script waits for a very short unit of time your music won’t load in time, but when you use the Stopped event, the script will get information to wait for the music to finish (when music comes to an end) and then will turn off your music.

I hope I explained it well :sweat_smile:

3 Likes