Need help with music system

I am currently editing a music system to use custom variables supplied by the developer; however, I seem to be experiencing problems doing so, is anyone able to help me?

The player keeps showing the same song name and artist and not on the right song.

Code (server script)

local enabled    = false
local groupId    = 100000
local groupRank  = 10

local mainSpeaker = Instance.new("Sound", workspace)
local sounds = script["Sounds"]
local idList = {}

script["MusicUpdate"].Parent = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(function(char)
		
	end)
	Player.Chatted:connect(function(message)
		if message:lower() == "skip" then
			if groupId > 0 then
				
				if Player:GetRankInGroup(groupId) >= groupRank then
					if mainSpeaker then
						mainSpeaker.TimePosition = mainSpeaker.TimeLength
					end
				end
			elseif Player.userId == game.CreatorId then
				if mainSpeaker then
					mainSpeaker.TimePosition = mainSpeaker.TimeLength
				end
			end
		end
	end)
end)

wait(5)

if #sounds:GetChildren() > 0 then
	for num,obj in pairs(sounds:GetChildren()) do
		local currentId = obj.SoundId or nil
		Artist = obj:WaitForChild("Artist").Value or nil
		Name = obj:WaitForChild("Name").Value or nil
		if currentId then
			idList[num] = currentId
		end
	end
end

mainSpeaker.Name = "MusicPlayer"
songNumber = 1

while(enabled and wait()) do
	mainSpeaker.TimePosition = 0
	
	if #idList > 0 then
		mainSpeaker.SoundId = idList[songNumber]
		mainSpeaker:Play()
		local event = game:GetService("ReplicatedStorage"):FindFirstChild("MusicUpdate")
		event:FireAllClients(Name,Artist)
	end
	
	songNumber = songNumber + 1
	if songNumber > #idList then
		songNumber = 1
	end
	mainSpeaker.Ended:wait()
end

Reciever (local script)
local Player = game.Players.LocalPlayer


local event = game:GetService("ReplicatedStorage"):WaitForChild("MusicUpdate")

event.OnClientEvent:connect(function(songname,artist)
	
		script.Parent.Text = songname.." by "..artist
	
end)
Placements

image

1 Like

I would suggest localizing your variables with local, global variables can cause this sort of problem:

local Artist = obj:WaitForChild("Artist").Value or nil
local Name = obj:WaitForChild("Name").Value or nil

This block of code is completely useless, because it really does nothing, that’s why only one song is displayed. This part of the code will change the name and artist until it reaches the last one, which would be the one displayed on peoples screens:

if #sounds:GetChildren() > 0 then
	for num,obj in pairs(sounds:GetChildren()) do
		local currentId = obj.SoundId or nil
		Artist = obj:WaitForChild("Artist").Value or nil
		Name = obj:WaitForChild("Name").Value or nil
		if currentId then
			idList[num] = currentId
		end
	end
end

Just simply iterate through the songs one at a time:

  local Event = game:GetService("ReplicatedStorage").MusicUpdate -- Gets the event

while true do
    for _, Song in ipairs(Sounds:GetChildren()) do -- Loops through the children
        Song:Play() -- Plays the song

        Event:FireAllClients(Song.Name.Value, Song.Artist.Value) -- Fires all clients
        Song.Ended:Wait() -- Waits for the song to finish
    end
end

Where would I put this? :smile: Thanks

You would put that in the server script.