Attempt to get length of a nil value(this is a error I have never encounted before)

You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
Im making a music changing script using a ModuleScript with MusicID’s

What is the issue?
Workspace.Others.BackroundMusic.MusicChanger:7: attempt to get length of a nil value

Music Changing Script:

local RepStor = game.ReplicatedStorage
local MusicModule = require(RepStor:WaitForChild("MusicModule"))
local MusicList = MusicModule.Music
local MusicInstance = script.Parent or workspace:WaitForChild("Others").BackroundMusic

while wait(1) do
	local count = Random.new():NextInteger(1, #MusicList)
	
	MusicInstance.SoundId = "rbxassetid://"..MusicList[count]
	
	if not MusicInstance.IsLoaded then
		MusicInstance.Loaded:Wait()
	end
	
	MusicInstance.Sound = 0.5
	MusicInstance.TimePosition = 0
	wait(.5)
	MusicInstance:Play()
	MusicInstance.Ended:Wait()
end

ModuleScript:

local Music = {
 1845554017,
 1836842889,
 1847853099,
 1843463175,
 1837861894,
 1843324336,
 1840005864,
 1842967125,
 4908301571
}

return Music
2 Likes

Your module script returns your table, Music. When you try to define MusicList, you are trying to get Music.Music, Which doesn’t exist (hence why it says get length of NIL value.)

You should replace MusicList with music module.

local RepStor = game.ReplicatedStorage
local MusicList = require(RepStor:WaitForChild("MusicModule"))
local MusicInstance = script.Parent or workspace:WaitForChild("Others").BackroundMusic

while wait(1) do
	local count = Random.new():NextInteger(1, #MusicList)
	
	MusicInstance.SoundId = "rbxassetid://"..MusicList[count]
	
	if not MusicInstance.IsLoaded then
		MusicInstance.Loaded:Wait()
	end
	
	MusicInstance.Sound = 0.5
	MusicInstance.TimePosition = 0
	wait(.5)
	MusicInstance:Play()
	MusicInstance.Ended:Wait()
end
1 Like