I have this for loop, it works for the most part but I have to spam it to work, it doesn't repeat for all the instances named "Sound"
x = game.Workspace.DJBoardPRO Audios = game.Workspace.DJBoardPRO:GetChildren() script.Parent.MouseButton1Down:connect(function() for i = 1, #Audios do if Audios[i].Name == "Sound" then x.Sound.Volume = "1" wait(0.05) x.Sound.Volume = "0.8" wait(0.05) x.Sound.Volume = "0.6" wait(0.05) x.Sound.Volume = "0.4" wait(0.05) x.Sound.Volume = "0.2" wait(0.05) x.Sound:Stop() wait(.1) x.Sound:Destroy() end end end)
Let's get your errors:
Using generic loops make you get all the contents of a table:
-- we don't need x value local Audios = game.Workspace.DJBoardPRO:GetChildren() script.Parent.MouseButton1Down:connect(function() for i,v in pairs(Audios) do if v.Name == "Sound" then v.Volume = "1" wait(0.05) v.Volume = "0.8" wait(0.05) v.Volume = "0.6" wait(0.05) v.Volume = "0.4" wait(0.05) v.Volume = "0.2" wait(0.05) v:Stop() wait(.1) v:Destroy() end end end)
You're setting a float value with a string one, so, let's change it - probably the main cause of your error
local Audios = game.Workspace.DJBoardPRO:GetChildren() script.Parent.MouseButton1Down:connect(function() for i,v in pairs(Audios) do if v.Name == "Sound" then v.Volume = 1 wait(0.05) v.Volume = 0.8 wait(0.05) v.Volume = 0.6 wait(0.05) v.Volume = 0.4 wait(0.05) v.Volume = 0.2 wait(0.05) v:Stop() wait(.1) v:Destroy() end end end)
Using another for loop cleans up your script and make it look more professional.
local Audios = game.Workspace.DJBoardPRO:GetChildren() script.Parent.MouseButton1Down:connect(function() for i,v in pairs(Audios) do if v.Name == "Sound" then for i = 1,0,-0.2 do v.Volume = i wait() -- waits 1/30 second, but you may set it to 0.05 end v:Stop() wait(.1) v:Destroy() end end end)
If you want to make the sounds fade out at the same time, you may use a coroutine, else, ignore this step:
local Audios = game.Workspace.DJBoardPRO:GetChildren() script.Parent.MouseButton1Down:connect(function() for i,v in pairs(Audios) do if v.Name == "Sound" then fadeOut = coroutine.create(function() -- creates a new thread, something like a ghost script for i = 1,0,-0.2 do v.Volume = i wait() -- waits 1/30 second end v:Stop() wait(.1) v:Destroy() end) coroutine.resume(fadeOut) end end end)