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

How do I make it so that when the W key isn't pressed, a sound stops?

Ey. Not that good with keydown scripts. Im trying to make it so that when you are driving, if you stop pressing the throttle (W key) then the sound will stop, similar to when you put your foot off the gas when driving, the engine isn't as loud. Not sure what I'm doing wrong.

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()

game:GetService("UserInputService").InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.W then
	script.Parent.Parent.F1.Engine.Playing = true
end
        end)

game:GetService("UserInputService").InputEnded:connect(function(input)
    if input.KeyCode == Enum.KeyCode.W then
	script.Parent.Parent.F1.Engine.Playing = false
    end

end)

1 comment

0

You'd probably want to use :Play() and :Stop() respectively. Also, it's not loud because your volume settings are low/the sound's Volume property is low.

3 answers

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()

game:GetService("UserInputService").InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.W then
    script.Parent.Parent.F1.Engine:Play()
end
        end)


game:GetService("UserInputService").InputEnded:connect(function(input)
    if input.KeyCode == Enum.KeyCode.W then
    script.Parent.Parent.F1.Engine:Stop()
    end

end)

90% Chance to work.

-- Hope i helped.

---- AIphanium

------ Edited the answer, and also, no need to accept this answer, just edited your script :l

3 comments

0

Or... you could just change the amount of noise when someone clicks "w".

0

The only problem is I want it to keep playing when the W is down, and not play when it stops.

0

Then for that you'll want to use :IsKeyDown()

Try checking out :IsKeyDown() event thing..

while wait() do
	if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.W) then
		if not script.Parent.Parent.F1.Engine.Playing then
			script.Parent.Parent.F1.Engine:Play()
		end
	else
		script.Parent.Parent.F1.Engine:Stop()
	end
end

1 comment

0

No, don't use wait as a condition and make the condition IsKeyDown.

You should use :Play() and :Stop()

ex:

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
UIS = game:GetService('UserInputService')

Sound = game:GetService('SoundService'):FindFirstChild('Sound') --Remember to define your sound

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		Sound:Play()--Remember to define your sound
	else
		Sound:Stop()--Remember to define your sound
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		Sound:Stop()--Remember to define your sound
	end
end)

1 comment

0

under line two it states an error - "attempt to index global 'Player' (a nil value)"

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