Error While Connecting Function to Sound:Ended Event

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

You have an extra pair of parentheses.

This is correct:

soundOutput.Ended:Connect(getSong)

When you’re connecting a function like that, you don’t need the parantheses like you would call it by itself. I know it autocompletes everything you write a function, but those are the cause of the error.

soundOutput.Ended:Connect(getSong). That should fix it.

You need to remove the parentheses calling getSong otherwise you are telling Lua to call the function and use its result (none) for the argument to :Connect.