Music Script Time.Length Not Working Correctly

So I got a music script and for some reason when I change wait(120) to wait(songs.TimeLength) the song plays, and stops on the same song. Here’s the script below

local songs = { --songid, name
	{290176752, "Yeah Yeah Yeahs - Heads Will Roll (JVH-C remix)"},
	{3314604270, "bbno$ & y2k - lalala"},
	{603146550, "Cartoon - On & On"},
	{674678896, "The Chainsmokers - Paris (Launchpad Remix)"},
	{473760808, "Alex Skrindo - Jumbo"},
	{678878009, "Bones - TakingOutTheTrash"},
	{2642540349, "Halogen - U Got That"},
	{2513053758, "Halsey - Without me"},
	{647317220, "Lukas Graham - 7 Years [Owen Remix]"},
}


local nowplaying = {}

local requests = {
	
}

function shuffleTable(t)
	math.randomseed(tick())
	assert(t, "shuffleTable() expected a table, got nil" )
	local iterations = #t
	local j
	for i = iterations, 2, -1 do
		j = math.random(i)
		t[i], t[j] = t[j], t[i]
	end
end

shuffleTable(songs)


local songnum = 0

function game.ReplicatedStorage.musicEvents.getSong.OnServerInvoke()
	return nowplaying
end

game.ReplicatedStorage.musicEvents.purchaseSong.OnServerEvent:connect(function(p)
	if p.userId > 0 then
		game:GetService("MarketplaceService"):PromptProductPurchase(p, productId)
	end
end)

local votes = {
	
}

function makesong()
	workspace:WaitForChild("TrelloSound"):Stop()
	wait()
	if #requests == 0 then
		songnum = songnum+1
		if songnum > #songs then
			songnum = 1
		end
		nowplaying = songs[songnum]
	else
		nowplaying = requests[1]
		table.remove(requests,1)
	end
	local thisfunctionssong = nowplaying[1]
	workspace.TrelloSound.SoundId = "rbxassetid://"..thisfunctionssong
	wait()
	workspace.TrelloSound:Play()
	game.ReplicatedStorage.musicEvents.newSong:FireAllClients(nowplaying)
	votes = {}
	wait(songs.TimeLength)
	if "rbxassetid://"..nowplaying[1] == "rbxassetid://"..thisfunctionssong then
		makesong()
	end
end

function countVotes()
	local c = 0
	for _,v in pairs(votes) do
		if v[2] == true then
			c = c +1
		end
	end
	local remainder = #game.Players:GetPlayers() - #votes
	print(remainder)
	print(#votes)
	local percent = (c + (remainder/2))/#game.Players:GetPlayers()
	game.ReplicatedStorage.musicEvents.voteChange:FireAllClients(percent)
	if percent <= 0.2 then
		--skip song
		makesong()
	end
	return
end

game.ReplicatedStorage.musicEvents.voteYes.OnServerEvent:connect(function(p)
	for _,v in pairs(votes) do
		if v[1] == p.Name then
			v[2] = true
			countVotes()
			return
		end
	end
	table.insert(votes, {p.Name, true})
	countVotes()
end)

game.ReplicatedStorage.musicEvents.voteNo.OnServerEvent:connect(function(p)
	for _,v in pairs(votes) do
		if v[1] == p.Name then
			v[2] = false
			countVotes()
			return
		end
	end
	table.insert(votes, {p.Name, false})
	countVotes()
end)
--skip song command

Group = 2888962 
RequiredRank = 14
game.Players.PlayerAdded:connect(function(Player)
	Player.Chatted:connect(function(msg)
		if Player:GetRankInGroup(Group) >= RequiredRank or Player.Name == "Player" or Player.Name == "Crossota" or Player.Name == "TimeFailured" or Player.Name == "railworks2" or Player.Name == "ExploreTheLocal" then
			if msg == 'skipsong'or msg == 'SkipSong' then 
				makesong()
			end
		end
	end)
end)

game:GetService('MarketplaceService').ProcessReceipt = function(details)
	for _, player in ipairs(game.Players:GetPlayers()) do
		if player.userId == details.PlayerId then
            if details.ProductId == productId then
				game.ReplicatedStorage.musicEvents.addSong:FireClient(player)
            end
        end	
    end
end

game.ReplicatedStorage.musicEvents.addSong.OnServerEvent:connect(function(p,id)
	if tonumber(id) ~= nil and game:GetService("MarketplaceService"):GetProductInfo(tonumber(id)).AssetTypeId == 3 then
		table.insert(requests, {tonumber(id), game:GetService("MarketplaceService"):GetProductInfo(tonumber(id)).Name})
	end
end)

makesong()

Can someone help me I don’t know if I am missing something to that or not.

1 Like

I don’t know if I get it wrong but you are getting the TimeLength of a table. In addition It’d be nice if you provided us if there is an error on the output.

It gave no errors, then after a while it gave an error that the addsong event was getting exhausted, and that’s about it.

I have a question are you trying to get the current sound’s TimeLenght or all of the table?

Yes

Also here’s what the error looked like.

So if you are trying to get the current sound’s TimeLenght can you try to select the current song from the table. Because as I see you are trying to get the TimeLength of the table.

	wait(songs.TimeLength)

If I did put wait(120) instead of wait (songs.TimeLength) it works just perfect.

How would I be able to do that?

Ummm. You can get it with the same way you get the currently playing sound. I mean, you got the currently playing song in the line above the wait(songs.TimeLength)

Ye, get the TimeLength from

workspace.TrelloSound.SoundId

and not from songs.TimeLength as Hexlinee has been saying. There’s no variable in the songs table with time length, you only have the IDs and song names in the table.

2 Likes

I’m trying that but I don’t know if I am putting it in correctly but I still can’t get it to work.

1 Like

Perhaps you should try saving the songs TimeLength in with each song so its in the table where the songs are and easily accessed.

I mean you could use the Ended event,

workspace.TrelloSound.Ended:Wait()
1 Like

So I did that, but now the song either plays close to the ending or it doesn’t play at all after one song.

EDIT I got it figured out

Ok, share the secret please. Your readers would like to know how in case they get the same problem.

1 Like

So where it was wait (songs.timelength) I switched that to workspace.TrelloSound.Ended:Wait()

Then for the next song to play I did
Under workspace.TrelloSound.Ended:Wait()

I put workspace.TrelloSound.TimePosition = 0

2 Likes