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

My audio script has a problem with playing?

Asked by
Aztralzz 169
4 years ago

This one should be easy to figure out. Basically, this script plays the music ID that is inputted to the textbox. The problem is, if the focus is lost and I keep pressing enter the sound keeps restarting. Any help with this?

1game:GetService('UserInputService').InputBegan:Connect(function(Input, onGui)
2        if Input.UserInputType == Enum.UserInputType.Keyboard then
3        if Input.KeyCode == Enum.KeyCode.Return and textBox.Focused then
4            local sound = game.Workspace.Sound
5            sound.SoundId = "rbxassetid://"..textBox.Text
6            sound:Play()
7end end end)

Thanks!

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

You can use Textbox.FocusLost to detect enter being pressed instead of UIS

1textBox.FocusLost:Connect(function(enter,input)
2    if enter or input then -- remove input if you only want the sound to play when pressing enter
3        local sound = game.Workspace.Sound
4         sound.SoundId = "rbxassetid://"..textBox.Text
5         sound:Play()
6    end
7end)

This should only run once until Focus is regained

0
Thank you so much! :D Aztralzz 169 — 4y
Ad

Answer this question