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

How to use KeyDown?

Asked by 9 years ago
local plr = game.Players.LocalPlayer

function onKeyDown(key, mouse)
    if key:lower() == "e" and ((game.Players.Player1.Character.Torso.Position - game.Workspace.Interactables.Switches.AA.Position).magnitude) < 100 then
        local lightName = "AA"
        for i, aa in pairs(game.Workspace.Interactables.Lights:FindFirstChild(lightName):GetChildren()) do
            if aa:FindFirstChild("Light").Brightness == 1000 then
                aa:FindFirstChild("Light").Brightness = 0
            else
                aa:FindFirstChild("Light").Brightness = 1000
            end
        end
    end
end

mouse.KeyDown:connect(function(key) onKeyDown(key, mouse) end)

Trying to get this to work when you push E and you are close enough to the button it'll turn off the lights. No output and doesn't even print 1. Its in a localscript in starterpack

Edited!

0
The magnitude works for sure. YellowoTide 1992 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

Try this:

function onKeyDown(key, mouse)
if key:lower() == "e" and ((game.Players.Player1.Character.Torso.Position - game.Workspace.Interactables.Switches.AA.Position).magnitude) < 100 then

-- Add rest of your stuff here

mouse.KeyDown:connect(function(key) onKeyDown(key, mouse) end)
0
No :/ Edited the question with your answer YellowoTide 1992 — 9y
0
It still doesn't print anything? ultimate055 150 — 9y
0
Nope :/ YellowoTide 1992 — 9y
0
I don't see the problem, it works for me. ultimate055 150 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Fixed :) If anyones wondering how its this script.

function keydown(k)
    if k:lower() == "e" and ((game.Players.Player1.Character.Torso.Position - game.Workspace.Interactables.Switches.AA.Position).magnitude) < 100 then
        for a, aa in pairs(game.Workspace.Interactables.Lights.AA:GetChildren())  do
            aa.Light.Brightness = 0
        end
    end
end

m = script.Parent.Parent:GetMouse();
m.KeyDown:connect(keydown)

Answer this question