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.
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, KeyDown
has 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!