Random music error

So I made a script (not local) that plays random music for a music zone. But I found an error when I played the game and it kept spamming the same thing.


Here’s my script

local sound = script.Parent.Ambiences.Ambience
local rs = game:GetService("RunService")
local music = {
	Track1 = {
		Name = "Chilled Relaxation",
		ID = 1839363112
	},
	Track2 = {
		Name = "Busy Vibes",
		ID = 1836393138
	},
	Track3 = {
		Name = "Soft Sounds",
		ID = 1840384233
	},
	Track4 = {
		Name = "Lo Fi Bay",
		ID = 9040313420
	}
}

local function getRandomTrack()
	local tracks = music
	local track = tracks[math.random(1, #tracks)]
	return track
end

rs.Heartbeat:Connect(function()
local randomTrack = getRandomTrack()
sound.SoundId = "rbxassetid://"..randomTrack.ID
sound:Play()
sound.Ended:Wait()
end)

and yes i didnt add a “while true do”

That happens when the minimum amount is higher than the maximum. Here is an example:

math.random(1, 4)

Works fine! :+1:

math.random(1, -4)

Not working :-1:

Try printing the tracks variable to see if it is empty or not.

2 Likes

It wasn’t empty.
Screenshot 2025-03-01 081724

You can’t get the number of elements from dictionary table using #. To solve the problem, you can simply remove the keys in music (Track1, Track2, Track3, Track4)

This is the revised code:

local sound = script.Parent.Ambiences.Ambience
local rs = game:GetService("RunService")
local music = {
	{
		Name = "Chilled Relaxation",
		ID = 1839363112
	},
	{
		Name = "Busy Vibes",
		ID = 1836393138
	},
	{
		Name = "Soft Sounds",
		ID = 1840384233
	},
	{
		Name = "Lo Fi Bay",
		ID = 9040313420
	}
}

local function getRandomTrack()
	local track = music[math.random(1, #music)]
	return track
end

rs.Heartbeat:Connect(function()
    local randomTrack = getRandomTrack()
    sound.SoundId = "rbxassetid://"..randomTrack.ID
    sound:Play()
    sound.Ended:Wait()
end)
1 Like

The error you’re getting is due to .lua not being able to get the length of a dictionary table (returning 0). You can use @Crynoxia_1’s method or check this post if you still want to use a dictionary table as your method.

1 Like

So, It kinda worked but it stopped the music.

because you are assigning a new id to the sound and playing it every heartbeat signal. instead you can just do that:

--!native
--!optimize 2
--!nocheck
local sound = script.Parent.Ambiences.Ambience
local music = {
	{
		Name = "Chilled Relaxation",
		ID = 1839363112
	},
	{
		Name = "Busy Vibes",
		ID = 1836393138
	},
	{
		Name = "Soft Sounds",
		ID = 1840384233
	},
	{
		Name = "Lo Fi Bay",
		ID = 9040313420
	}
}

local function getRandomTrack()
	return music[math.random(1, #music)]
end

while true do
	sound.SoundId = "rbxassetid://"..getRandomTrack().ID
	sound:Play()
	sound.Ended:Wait()
end

Heartbeat doesn’t wait for any intervals (unless you use debounce which I don’t recommend). So the Ended function doesn’t do any effect.

Since you don’t want to use any while true loops, I recommend making a function that loops through itself.

local function pSound()
	local randomTrack = getRandomTrack()
	warn(randomTrack)
	sound.SoundId = "rbxassetid://"..randomTrack.ID
	sound:Play()
	sound.Ended:Wait()
	pSound()
end
pSound()
3 Likes

Check if properties of sound is wrong or if other script is interrupting the sound.

2 Likes

Something that I also recommend you doing (since Roblox has this “Issue” for a long time) is to also make a line before the Play() and add sound.TimePosition = 0, that way the music doesn’t stars at its middle or end.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.