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

How do I 'change' something when I press a key, and 'change' it back when I press the key again?

Asked by
lmAnon 2
6 years ago

How do I make it so that, for example, when I press a key, the transparency of a part changes to 1, and when I press again, it changes to 0? I use Keyboardinput for this, and I want to know how to 'change' something when I press a key, and 'change' it back when I press it again.

Here you can see what I'm trying to do..

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R then
        Tool.examplepartthing.Transparency = 0.65
        wait()

    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)
0
( I actually already know the part where you can 'change' something when you press it, but I really need to know how to change it back when you press it again lmAnon 2 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

You could try something like this :

didpress = false

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R then
    if didpress == false then
        didpress = true
        Tool.examplepartthing.Transparency = 1
             wait()
    else
        didpress = false
        Tool.examplepartthing.Transparency = 0
    end
    end
end

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

Please note I made this script directly in the answer, not in Roblox Studio, so I don't know if it'll actually work or not. :P

0
Thank you very much. It actually worked! lmAnon 2 — 6y
0
You're welcome! :D User#20279 0 — 6y
Ad

Answer this question