Recentely I was working on an audio vizualizer game. What I was trying to do is once a sound finishes playing it gets deleted. As the audio vizualizer script runs on
local Sound = game.Workspace.Sound.PlaybackLoudness
so it is dedicated to one object called “Sound” but once it finishes playing, the player writes in a new ID for the song and an instance causes a new “Sound” to happen. And since a new sound is made the vizualizer cannot decide which PlaybackLoudness to choose so the frame just glitches out and becomes tiny.
So what I am trying to do is to make a script that will remove the sound when it finishes playing.
VIZUALIZER SCRIPT:
local Frame = script.Parent
----------------------------------
game:GetService("RunService").RenderStepped:Connect(function(Doge)
local Sound = game.Workspace.Sound.PlaybackLoudness
Frame.Size = UDim2.new(0,Sound,0,Sound)
end)
Audio ID script:
local soundTextBox = script.Parent
soundTextBox.FocusLost:connect(function(enterPressed)
if enterPressed then
local ContentProvider = Game:GetService("ContentProvider")
local sound = Instance.new("Sound")
sound.SoundId = ContentProvider.BaseUrl .. "asset/?id=" .. soundTextBox.Text
sound.Parent = game.Workspace
while (ContentProvider.RequestQueueSize > 0) do
wait()
end
sound:Play()
end
end)
This is absolutely the best answer and hey what’s up Blokav, but if you want to wait the TimeLength, why not just do wait(Sound.TimeLength)? Or better yet, include the PlaybackSpeed for further accuracy wait(Sound.TimeLength * Sound.PlaybackSpeed)?
That would definitely work, but I personally find an event-driven structure more reliable than using the wait function, whose actual duration may vary slightly depending on the speed of the game.
Sorry to bump this up, but I am in need of finding the timelength in my script, however I’m doing wait(sound.TimeLength) and I was wondering if anyone has found a way to make it work?
At the moment whenever I try to identify the TimeLength, by doing:
music = script.Parent -- The sound
timetowait = script.Parent.TimeLength
while true do
music.SoundId = "rbxassetid://" -- The actual asset ID is within the script
wait(0.1)
print(timetowait)
wait(10)
end
It comes up with 0, I have no idea why this is happening and have tried all solutions on this post.
Found a fix! You have to wait until the audio has finished loading, and then like you said, to move the “timetowait” variable being defined to after the sound has loaded.
I’ve completely redone the script, however you can look at the new one here if anyone has the same issue.
local audio = script.Parent.Sound
while true do
audio.SoundId = "rbxassetid://" -- Put in sound ID after ''rbxassetid://''
if not audio.isLoaded then
audio.Loaded:wait()
end
TimeWait = audio.TimeLength
print(TimeWait)
audio:Play()
wait(TimeWait)
print("Audio has finished playing, restarting.")
wait(1)
end