Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Music script help?

Asked by
FiredDusk 1466 Moderation Voter
9 years ago

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

Log in to vote
0
Answered by 9 years ago

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.

Ad

Answer this question