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!
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)
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.
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
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!
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
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)