Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I fix the "Play Sound" button in my admin panel? and the rest- too long

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)

4 comments

0

ok so... in local sound do local sound = game.Workspace.Sound

0

not sure what the point of sound.SoundId = "" is because when you stop the sound it wont play and when you enter a new one it will automatically clear it

0

"workspace" is the same as "game.Workspace" but shorter

0

Just replying to your "not sure what the point of sound.SoundId = "" is ... ": Ohhh ok!

2 answers

✓ Accepted answer

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.

Sources


###GetPropertyChangedSignal ###Sound.Ended

1 comment

0

Thanks! It worked! For y'all internet surfers out there (I'm the original poster of this question), I'd recommend doing "Sound.Ended" because it's easier & doesn't require any checks.

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

1 comment

0

I put the sound inside of workspace so everyone inside a server would hear it (aka. global sounds) and that putting it inside of StarterGui makes it local which is not what I want & yes, the Id or... what I call, "input" is a text box, don't worry.

This discussion is preserved as it was. New answers, comments, and votes are closed.