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

How Would You Play A Sound In A Specific Order?

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

✓ Accepted answer

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!

2 comments

0

Thanks, I usually use keydown since it's more simple but I'll get into the new methods

0

Np. Browse the wiki for UserInputService to learn more about how it works and how to use it.

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