Hello, recently I was just testing out my music system and it seemed to just keep playing the same song over and over again. Though everything works with queue, and playing and skipping songs!
Why is the song repeating its self?
Here is the code →
--[[
@author TwinPlayzDev_YT
@credit SlipperySpelunky/SaifLachmann (Dev Forum Helpers), Fexzi, C9 (Discord Helpers)
@since 1/28/22
This script basically controls the music player.
--]]
--[ SERVICES ]--
local Players = game:GetService("Players") -- The Player service.
local rep = game:GetService("ReplicatedStorage") -- The ReplicatedStorage service.
local MarketplaceService = game:GetService("MarketplaceService") -- The Marketplace service.
--[ MAIN LOCALS ]--
local CompletedBindable = Instance.new("BindableEvent")
CompletedBindable.Name = "CompletedBindable"
CompletedBindable.Parent = game:GetService("ServerStorage")
local events = rep.EVENTS.MusicEvents -- This variable is the folder in which the remote events/functions are stored in.
local queue = {}
local songs = {
2759694106,
}
local currentSong = "No song playing"
local sound = game.Workspace:FindFirstChild("GlobalSound")
--[ GET PLAYING SONG FUNCTION ]--
events.GetPlayingSong.OnServerInvoke = function()
return currentSong -- When the GetPlayingSong remote function gets invoked, we will return the currently playing song name back to the client who invoked it.
end
--[ SONG REQUEST FUNCTION ]--
events.RequestSong.OnServerEvent:Connect(function(player, id)
print("REQUEST SONG WAS FIRED : " .. id)
if id then
local success, data = pcall(MarketplaceService.GetProductInfo, MarketplaceService, id)
if success and data and data.AssetTypeId == 3 then
table.insert(queue, id) -- inserting Id into queue table
events.RefreshQueue:FireAllClients(queue) -- firing refresh queue for later
end
end
end)
--[ MAIN PLAYER ]--
while true do
if #songs > 0 then -- We first check if there are any songs implemented into the system. If so, we proceed.
sound.TimePosition = 0 -- We set the TimePosition property of our sound object to 0. This is done so when a new song starts playing, it doesn't continue from the TimePosition of when the previous song ended at.
local selectedSong -- We create our selectedSong variable. It will be properly declared in the following code.
if #queue > 0 then -- We check if there are any requested songs. If so, we select the first song that was requested.
selectedSong = queue[1] -- Sets the selected song variable as the requested song.
table.remove(queue, 1) -- Removes the selected song from the queue table.
events.RefreshQueue:FireAllClients(queue) -- We fire all clients with the RefreshQueue remote event to refresh the queue as it was modified.
else
selectedSong = songs[math.random(1, #songs)] -- If there are no requested songs, then we select a random song from our songs table.
end
sound.SoundId = "rbxassetid://"..tostring(selectedSong) -- We set the SoundId property of the sound object to our selected song's ID.
local count = 0
repeat
wait(.1)
count = count + .1
until sound.IsLoaded or count >= 4
if sound.IsLoaded then
sound.PlaybackSpeed = 1 -- We set the PlaybackSpeed property of the sound to the Pitch index of our selected song. If there is no pitch index, then we set it to 1.
sound:Play() -- We begin playing the song.
events.NewSong:FireAllClients(selectedSong) -- We fire the NewSong event to all clients to indicate a new song has started playing.
currentSong = selectedSong -- We set the currentSong variable to the name of the selected song.
--repeat wait() until not sound.IsPlaying
CompletedBindable.Event:Wait()
end
else -- if no songs playing we break out of the loop at 98 and change the text of the sign
break
end
end
--if sound.TimePosition == sound.TimeLength or not sound.IsPlaying then
-- CompletedBindable:Fire()
--end
sound.Ended:Connect(function() CompletedBindable:Fire() end)
sound.Stopped:Connect(function() CompletedBindable:Fire() end)
Recently, I also added the bindable event to help fix the skipping problem, but now for some reason the selectedsong is the same song over and over again. I don’t think my script knows if the sound has ended or not?
Properties of sound →
What could be the issue?