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

Music script help?

Where would I put a "Stop" so when I click a text button, the music stops?

local ContentProvider = game:GetService("ContentProvider")

local totalWait = 20 --only wait for 20 seconds max

local queueSize = ContentProvider.RequestQueueSize

function UpdateQueueSize()
	queueSize = ContentProvider.RequestQueueSize
end

function WaitForSoundToLoad()
	local start = tick()
	while (ContentProvider.RequestQueueSize > queueSize) and ((tick() - start) < totalWait ) do
		wait(0.1)
	end
	if (start - tick() > 1) then print("Took " .. tostring(start - tick()) " seconds to load") end
end

function PlaySound(id)
	if id == nil then print("Invalid sound") return end
	local sound = Instance.new("Sound")
	sound.SoundId = "http://www.roblox.com/asset?id=" .. tostring(id)
	UpdateQueueSize()
	sound.Parent = game.Players.LocalPlayer.PlayerGui
	WaitForSoundToLoad()
	sound:Play()
end

local TextBox = script.Parent:WaitForChild("TextBox")
local TextButton = script.Parent:WaitForChild("TextButton")

TextBox.FocusLost:connect(function(enterPressed)
	Game:GetService("ContentProvider"):Preload(TextBox.Text)
end)

TextButton.MouseButton1Click:connect(function()
	PlaySound(tonumber(TextBox.Text))
end)

1 answer

✓ Accepted answer

You could use a wait(int), where int is the integer or interval of how long the sound is, and then put the stop after that.

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