Nil value in a function

Hey. I’m making a script for a game that lowers the current music playing’s volume to 0, before playing the desired music. I set up the function in a module script in an attempt to streamline the process. However, I’m having trouble, as apparently one of my parameters is a nil value.

Here is the code. The error is on line three:

local MusicModule = {}

MusicModule.PlaySong(function(song: Sound, event: BindableEvent, currentSong: Sound)
	
	event.Event:Connect(function()
		
		if currentSong.Volume ~= 0 then
			
			for i = 0.3, 0, 0.01 do

				currentSong.Volume = i
				wait(0.01)
				print(currentSong.Name .. " set to " .. tostring(currentSong.Volume) .. " volume")

			end
			
		end

		currentSong:Stop()
		
		song:Play()
		
	end)
	
end)

return MusicModule

Thank you all in advance!

Try:

local MusicModule = {}

function MusicModule.PlaySong(song: Sound, event: BindableEvent, currentSong: Sound)
	
	event.Event:Connect(function()
		
		if currentSong.Volume ~= 0 then
			
			for i = 0.3, 0, 0.01 do

				currentSong.Volume = i
				wait(0.01)
				print(currentSong.Name .. " set to " .. tostring(currentSong.Volume) .. " volume")

			end
			
		end

		currentSong:Stop()
		
		song:Play()
		
	end)
	
end

return MusicModule

Then call your function somewhere like:

local module = require(path_to_music_module)

module.PlaySong(song, the_bindable_event_here, currentSong)

This is just a 1 : 1 copy of my code? The way I call the function is the same too

My bad. Did you try it at least?

Differences:

Yours:

MusicModule.PlaySong(function(song: Sound, event: BindableEvent, currentSong: Sound)

Mine:

function MusicModule.PlaySong(song: Sound, event: BindableEvent, currentSong: Sound)

When you do it your way, you are immediately calling your function without any parameters.
Given the additional information you gave where you stated that the way you call the function is the same too is an indicator that you didn’t set up your function correctly.

1 Like

I think its

local MusicModule = {}

function MusicModule.PlaySong(song: Sound, event: BindableEvent, currentSong: Sound)
	
	event.Event:Connect(function()
		
		if currentSong.Volume ~= 0 then
			
			for i, number do
                                Local Number = 0.3, 0, 0.01
				currentSong.Volume = 0
				wait(0.01)
				print(currentSong.Name .. " set to " .. tostring(currentSong.Volume) .. " volume")

			end
			
		end

		currentSong:Stop()
		
		song:Play()
		
	end)
	
end

return MusicModule

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
memory leak!!!

Maybe?

local MusicModule = {}

MusicModule.PlaySong(function(song: Sound, event: BindableEvent, currentSong: Sound)
	
	event.OnServerEvent:Connect(function()
		
		if currentSong.Volume ~= 0 then
			
			for i = 0.3, 0, 0.01 do

				currentSong.Volume = i
				wait(0.01)
				print(currentSong.Name .. " set to " .. tostring(currentSong.Volume) .. " volume")

			end
			
		end

		currentSong:Stop()
		
		song:Play()
		
	end)
	
end)

return MusicModule

Thank you all in advance!

Replace Connect with Once, your connecting to it every time PlaySong is ran

Are you sure I think it would just be connect

maybe this?

local MusicModule = {}

MusicModule.PlaySong(function(song: Sound, event: BindableEvent, currentSong: Sound)
	
	event.OnServerEvent:Connect(function(NewSong)
		
		if currentSong.Volume ~= 0 then
			
			for i = 0.3, 0, 0.01 do

				currentSong.Volume = i
				wait(0.01)
				print(currentSong.Name .. " set to " .. tostring(currentSong.Volume) .. " volume")

			end
			
		end

		currentSong:Stop()
		
		song:Play()
		
	end)
	
end)

return MusicModule

That’s for RemoteEvents though, BindableEvents only have .Event or :Fire

Oh, I I didn’t know there was a thing “bindable Event”

then

local MusicModule = {}

MusicModule.PlaySong(function(song: Sound, event: BindableEvent, currentSong: Sound)
	
	event.Event:Connect(function(NewSong)
		
		if currentSong.Volume ~= 0 then
			
			for i = 0.3, 0, 0.01 do

				currentSong.Volume = i
				wait(0.01)
				print(currentSong.Name .. " set to " .. tostring(currentSong.Volume) .. " volume")

			end
			
		end

		currentSong:Stop()
		
		song:Play()
		
	end)
	
end)

return MusicModule

Yeah, what @EmberSquared is saying is you should replace

event.Event:Connect(function(NewSong)

with

event.Event:Once(function(NewSong)

Because :Once disconnects the function after firing while :Connect just kinda stays there.

(Note to everyone: If you’re using :Once, you’ll have to call the function every time, whereas if you use :Connect you can run the function once and have it active forever)

1 Like

That try @EmberSquared and if it works set it to the solution

1 Like

That’s my bad. I’ll try this, @EmberSquared and @Magic_Crash 's to see if they work.

1 Like

Worked. Thanks so much, and sorry for the misunderstanding :slightly_smiling_face: