The issue is, is that, when I try to click the "Play Sound" button, (even on like... 1 hour long sounds) it just immediately changes the text to "Stop Sound" and another microsecond later it changes to "Play Sound" without playing the audio. The things I tried is inside the code block below.
local Module = require(game.StarterGui.eText) local btn = script.Parent.Parent.BtnPlay local input = script.Parent.Parent.Input local sound = workspace.Sound local playing = false btn.MouseButton1Click:Connect(function() if playing == false then -- (<-- Which is this one) I tried to make it easier for the script to understand but didn't work, the previous one was -- "if not playing then" sound.SoundId = "rbxassetid://" .. input.Text sound:Play() Module.ApplyText(btn, "Stop Sound") playing = true wait(sound.TimeLength) -- If I put it to 8 seconds, the sound will play for 8 seconds but -- I want it to wait for the sounds length but doesn't work sound:Stop() sound.SoundId = "" Module.ApplyText(btn, "Play Sound") Module.ClearText(input) playing = false elseif playing == true then sound:Stop() sound.SoundId = "" Module.ApplyText(btn, "Play Sound") Module.ClearText(input) playing = false end end)
i could be wrong but it could be because the sound is not yet loaded when you index it's TimeLength
(If it's not loaded it will return 0), that's because roblox must first access it's servers and get product info of the sound and then only configure the sound. You have 2 solutions here, GetPropertyChangedSignal or Sound.Ended.
GetPropertyChangedSignal
This is a signal that fires when given property of instance changes, in your case you can wait until the sound's length is not 0:
... sound.SoundId = "rbxassetid://" .. input.Text sound:Play() Module.ApplyText(btn, "Stop Sound") playing = true Sound:GetPropertyChangedSignal('TimeLength'):Wait() --/Will wait until this event fires, it will fire when TimeLength --\of this sound changes, i can't be sure that this is realiable --\so i would better do more checks or use the second method wait(Sound.TimeLength) sound:Stop() ...
Sound.Ended
An event of Sound
which fires when it ends, in your case you would use it instead of wait
and yield until the event fires:
... sound.SoundId = "rbxassetid://" .. input.Text sound:Play() Module.ApplyText(btn, "Stop Sound") playing = true sound.Ended:Wait() ...
That way you can also get rid of Sound:Stop()
and i personally prefer this method over the first one.
just put the sound inside the gui and the play and stop in different buttons like:
function Song() local ID = script.Parent.Parent.id.Text local sound = script.Parent.Parent.Parent.Music Sound.SoundId = 'rbxassetid://'.. ID Sound:Play() end script.Parent.MouseButton1Click:connect(Song)
and then
function Stop() script.Parent.Parent.Parent.Sound:Stop() end script.Parent.MouseButton1Click:connect(Stop)
-side note make sure the sounds in the gui and the buttons are in a frame id is a text box! :)