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

How Would You Play A Sound In A Specific Order?

Asked by 6 years ago
Edited 6 years ago
local player = game.Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()
start = script.startsound
off = script.offsound
on = script.On --boolvalue

mouse.KeyDown:Connect(function(key)
    if key == "f" then
        if on.Value == false then
            if start.IsPlaying == false then
                start:Play()
                on.Value = true
            elseif on.Value == true then
                on.Value = false
                off:Play()
            end
        end
    end
end)

What I want this script to do is to play a sound in a specific order. A player presses f and the start sound plays. Then he presses f and the off sound plays. I tried using a boolvalue to toggle it but I didn't know how to use it with key functions.

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

What I usually do is insert a BoolValue inside of the script to make it more easier for me.

function onKeyPress(inputObject, gameProcessedEvent)
    local playing = script.playing
    if inputObject.KeyCode == Enum.KeyCode.F then
        playing.Value = not playing.Value --true/false
        if playing.Value == true then
        script.sound:Play()
        script.offsound:Stop()
    elseif playing.Value == false then
        script.sound:Stop()
        script.offsound:Play()
    end
end
    end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Also, KeyDownhas been deprecated for a while now. It's now best to use UserInputService.If this helped, please accept answer! If it didn't, please comment! Thanks!

0
Thanks, I usually use keydown since it's more simple but I'll get into the new methods InfernoExeuctioner 126 — 6y
0
Np. Browse the wiki for UserInputService to learn more about how it works and how to use it. PyccknnXakep 1225 — 6y
Ad

Answer this question