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

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

Asked by 6 years ago

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)

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. User#19524 175 — 6y

3 answers

Log in to vote
0
Answered by
AIphanium 124
6 years ago
Edited 6 years ago
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

0
Or... you could just change the amount of noise when someone clicks "w". AIphanium 124 — 6y
0
The only problem is I want it to keep playing when the W is down, and not play when it stops. TheBeaver101 28 — 6y
0
Then for that you'll want to use :IsKeyDown() User#19524 175 — 6y
Ad
Log in to vote
0
Answered by
thesit123 509 Moderation Voter
6 years ago
Edited 6 years ago

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
0
No, don't use wait as a condition and make the condition IsKeyDown. User#19524 175 — 6y
Log in to vote
0
Answered by 6 years ago

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)

0
under line two it states an error - "attempt to index global 'Player' (a nil value)" TheBeaver101 28 — 6y

Answer this question