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 make it so I can detect a second key press of the same key?

Hi,

I'm a little confused about key input. I'm making a light script, and I want it so when you press E once, the light begins flashing - but when you press E again, the light stops flashing.

I am not the greatest when it comes to key input :p

Here's my code so far:

local car = script.Parent.Car.Value
local mouse=game.Players.LocalPlayer:GetMouse()
right = false
mouse.KeyDown:connect(function(key)
	if key=="e" then
		if not right == true then
			right = true
		while right == true do
		wait(1/27)
		car.Body.RIGHT.Material = "Neon"
		car.Body.RIGHT.Transparency = 0
		wait(1/27)
		car.Body.RIGHT.Material = "SmoothPlastic"
		car.Body.RIGHT.Transparency = 1
		end
		end
	end
end)

What would I do to make it so that once you press it again, the light stops flashing?

1 answer

✓ Accepted answer

Well first let's add a variable that either says the lights are on or not

local lights=false

Next let's add it to the start of your script

local car = script.Parent.Car.Value
local mouse=game.Players.LocalPlayer:GetMouse()
local lights = false
mouse.KeyDown:connect(function(key)

end)

Now let's check if the lights are on or not if on do this if not then do that

local car = script.Parent.Car.Value
local mouse=game.Players.LocalPlayer:GetMouse()
local lights = false
mouse.KeyDown:connect(function(key)
	if lights==false then
		
	elseif lights==true then
		
	end
end)

Now let's tell it what to do if the lights are off, also in the loop we will check that lights is still == to true

local car = script.Parent.Car.Value
local mouse=game.Players.LocalPlayer:GetMouse()
local lights = false
mouse.KeyDown:connect(function(key)
	if key=="e" then
		if lights==false then lights=true
			while lights == true do
			        wait(1/27)
			        car.Body.RIGHT.Material = "Neon"
			        car.Body.RIGHT.Transparency = 0
			        wait(1/27)
			        car.Body.RIGHT.Material = "SmoothPlastic"
			        car.Body.RIGHT.Transparency = 1
		        end
		elseif lights==true then lights=false
			
		end
	end
end)

Now what happens when its false

local car = script.Parent.Car.Value
local mouse=game.Players.LocalPlayer:GetMouse()
local lights = false
mouse.KeyDown:connect(function(key)
	if key=="e" then
		if lights==false then lights=true
			while lights == true do
			        car.Body.RIGHT.Material = "Neon"
			        car.Body.RIGHT.Transparency = 0
			        wait(1/27)
			        car.Body.RIGHT.Material = "SmoothPlastic"
			        car.Body.RIGHT.Transparency = 1
				wait(1/27)
		        end
		elseif lights==true then lights=false
			car.Body.RIGHT.Material = "SmoothPlastic"
			car.Body.RIGHT.Transparency = 1
		end
	end
end)

3 comments

0

Thank you very much!! I understand now, and the script you provided works perfectly.

0

Using key == "e" is extremely ineffective, and not recommended. Use the ContextActionService or UserInputService instead.

0

^ forgot about that.

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