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

How to use KeyDown?

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!

1 comment

0

The magnitude works for sure.

2 answers

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)

4 comments

0

No :/ Edited the question with your answer

0

It still doesn't print anything?

0

Nope :/

0

I don't see the problem, it works for me.

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)

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