I need help with a music script!

I made a script to play more songs and it doesn’t work, why?

Script:

local sound1 = 2107586824
local sound2 = 469746200
local sound3 = 409850382
local sound4 = 978616673
local sound5 = 912016304
local sound6 = 1910920326
local sound7 = 1786553127
local sound8 = 603146550
local sound9 = 2240509500
local sound10 = 859861483
local music = script.Parent

while true do
wait()
music.SoundId = “rbxassetid://”…sound1
music:Play()
music.Ended:Wait()
music.SoundId = “rbxassetid://”…sound2
music:Play()
music.Ended:Wait()
music.SoundId = “rbxassetid://”…sound3
music:Play()
music.Ended:Wait()
music.SoundId = “rbxassetid://”…sound4
music:Play()
music.Ended:Wait()
music.SoundId = “rbxassetid://”…sound5
music:Play()
music.Ended:Wait()
music.SoundId = “rbxassetid://”…sound6
music:Play()
music.Ended:Wait()
music.SoundId = “rbxassetid://”…sound7
music:Play()
music.Ended:Wait()
music.SoundId = “rbxassetid://”…sound8
music:Play()
music.Ended:Wait()
music.SoundId = “rbxassetid://”…sound9
music:Play()
music.Ended:Wait()
music.SoundId = “rbxassetid://”…sound10
music:Play()
music.Ended:Wait()
end

First things first you don’t need an Infinite loop to play music. Its really inefficient.

I may be wrong, but it’s most likely because you’re trying to concatenate a string with an integer and also that the song data was having trouble being downloaded. Here’s a fixed version. I’ve also made a few edits:

local ids = {2107586824} -- Insert the rest of your ids in this table 
local soundObject = script.Parent


while true do
    for _, v in pairs(ids) do -- Please use a for loop instead of painfulling writing all those lines
		local _= pcall(function() -- We want to wrap this in a pcall to prevent errors from the downloading errors breaking the script
			soundObject.SoundId = "rbxassetid://"..tostring(v)
			soundObject:Play()
			soundObject.Ended:Wait()
		end)
		
		wait()
	end
end

You are wrong. Lua will attempt to tostring the operands presented to it. Concatenating an integer to a string isn’t the problem. See this for more details regarding concatenation:
https://www.lua.org/pil/3.4.html

Other than the useless pcall because download errors do not terminate the execution of a script and that you should be using ipairs as you’re working with an array and not a dictionary, your code is still more preferable because it allows for arbitrary additions and removals to the playlist rather than hardcoding the playlist in the loop body.

1 Like