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

key down question?

okay I want a player to press a letter but I don't want it In a tool or anything I just want it to be you press a letter then I write my code of what I want to happen. Trust me I seached it up and I found some script that people said worked but they didn't when I tested them. Please don't down vote i seached it up here on the site and I found nothing. It would help if you explained it to me, what you did in the script.

Please help!

1 comment

2 answers

Try this:

player = game.Players.LocalPlayer
mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key == "q" then
		-- do stuff
	end
end

7 comments

0

I tried that before and put my code in there nothing happened

0

Non, change line 5 to this: "if key:lower(key) == "q" then" Im not saying it doesnt work, im just saying that you could improve it.

0

Ewarr, if you want Non's example to work, it has to be in a localscript :/

0

I know it does why do you think I tagged local script.

0

Dude is it just supposed to be in workspace or what? because it's not working!

0

Localscripts have to be in the player, see here: http://wiki.roblox.com/index.php?title=API:Class/LocalScript

0

Is something wrong with roblox studio or is it the script? It's not working?

I prefer using the UserInputService to get when a key is pressed because the mouse fires later and sometimes doesn't fire if you press and release fast enough like this

function KeyDown(key)
	print("The "..key.." key was pressed")
end
local ui=game:GetService("UserInputService")
ui.InputBegan:connect(function(obj)
	if obj.KeyCode~=nil then
		local K=string.char(obj.KeyCode)
		KeyDown(K)
	end
end)

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