How to run code when player presses hot key (Cntrl) if that does work then like shift or E

i looked at an article of inputservice and read some dev forum tutorials but after using the code suggested it didnt work.

image

im trying to make a flashlight connected to a part which the players camera is located in. im going to do this by making a debounce and setting the debounce to the opposite so when the key is pressed the script plays and disables/enables the flashlight. i also want a sound to play

the code:

local UserInputService = game:GetService("UserInputService")
local debounce = false
UserInputService.InputBegan:Connect(function(InputObject, Processed)
	if InputObject.KeyCode == Enum.KeyCode.E then
		if debounce == false then
			script.Parent["Flashlight Click"]:Play()
			wait(0.5)
			script.Parent.Enabled = false
		elseif debounce == true then
			script.Parent.Enabled = true
			script.Parent["Flashlight Click"]:Play()
			wait(0.5)
			debounce = false
		end	
	end
end)

Where its located:
image
im pressing E (NOTHING)

Hi. I assume that the camera part is located in the workspace.

LocalScripts will not run in the workspace. LocalScripts must be a descendant of the player(Player or Character) in order to run. If the camera part is located in the workspace, update the location of the local script and change the references in the local script.

So, First of all the logic for your entire code if off, so lets see it like this in the beginning the “debounce” is false so then when the user presses ‘E’ it will fire but the Spot Light will get disabled.

local UserInputService = game:GetService("UserInputService")
local debounce = false
UserInputService.InputBegan:Connect(function(InputObject, Processed)
	if InputObject.KeyCode == Enum.KeyCode.E then
		if debounce == true then
			script.Parent["Flashlight Click"]:Play()
			wait(0.5)
			script.Parent.Enabled = false  
            debounce = false
		elseif debounce == false then
			script.Parent.Enabled = true
			script.Parent["Flashlight Click"]:Play()
			wait(0.5)
			debounce = true
		end	
	end
end)

Im assuming that Camera Part is a child of Camera, so it will work.

1 Like

it turns off but doesnt turn back on. i still hear the click when it gets pressed but it doesnt change anything BTW: FLASHLIGHT IS A SPOTLIGHT

Insert a while true loop into the mix and it will work :smiley:

instead of using debounce you can use something similar to debounce its easier and better in my opinion

local UserInputService = game:GetService("UserInputService")
FlashEnable = false
UserInputService.InputBegan:Connect(function(InputObject, Processed)
	if InputObject.KeyCode == Enum.KeyCode.E then
                       FlashEnable = not FlashEnable 
                        if FlashEnable then
                        -- Open the flashlight, play sound etc
                        else
                        -- close flashlight stop sounds etc
	end
end)

how would i use the input key cntrl