I am scripting a simple radio to play music on my game that uses a function to generate the next song so I can later call the function at any time to generate a new song ID. This means I’m trying to avoid using while loops for this script.
The issue I’m facing is with an error that displays when the connection for the Sound:Ended() event. This error does not impact the script’s functionality until the first song ends but the error appears when the script is first triggered. As of now, I have not put any cooldown to wait for the server to load before triggering the event.
I’ve searched the Roblox Developer Hub for insights on the error but couldn’t find any posts about this. My function is declared before the connection and spelling had been checked prior to this post.
Code:
local soundIds = {4334254761,1843382633}
local marketplaceservice = game:GetService("MarketplaceService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local sendNotification = replicatedStorage:WaitForChild("sendNotification")
local soundOutput = Instance.new("Sound")
soundOutput.Parent = game.Workspace
soundOutput.Name = "Sound Output"
local randomNumber = nil
local currentId = nil
local asset = nil
local title = "Now Playing"
local text = nil
local duration = 15
local function getSong()
randomNumber = math.random(#soundIds)
currentId = soundIds[randomNumber]
soundOutput.SoundId="rbxassetid://"..currentId
asset = marketplaceservice:GetProductInfo(currentId)
print(asset.Name.." - "..asset.Creator.Name)
soundOutput:Play()
text = asset.Name.." - "..asset.Creator.Name
sendNotification:FireAllClients(title,text,duration)
end
soundOutput.Ended:Connect(getSong())
Code relating to the RemoteEvent and Marketplace Service did not change the outcome when removed
Output Window:
Wonderful Day - ROBLOX
21:17:35.311 - Attempt to connect failed: Passed value is not a function
21:17:35.312 - Stack Begin
21:17:35.312 - Script 'ServerScriptService.Script', Line 29
21:17:35.313 - Stack End