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

How do I make it so I can detect a second key press of the same key?

Asked by 6 years ago

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

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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)
0
Thank you very much!! I understand now, and the script you provided works perfectly. TomHark 9 — 6y
0
Using key == "e" is extremely ineffective, and not recommended. Use the ContextActionService or UserInputService instead. AstrealDev 728 — 6y
0
^ forgot about that. DanzLua 2879 — 6y
Ad

Answer this question