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

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

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)

1 comment

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

1 answer

✓ Accepted answer

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

2 comments

0

Thank you very much. It actually worked!

0

You're welcome! :D

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