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

key down question?

Asked by 9 years ago

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!

2 answers

Log in to vote
0
Answered by 9 years ago

Try this:

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

mouse.KeyDown:connect(function(key)
    if key == "q" then
        -- do stuff
    end
end
0
I tried that before and put my code in there nothing happened Ewarr1011 0 — 9y
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. groovydino 2 — 9y
0
Ewarr, if you want Non's example to work, it has to be in a localscript :/ groovydino 2 — 9y
0
I know it does why do you think I tagged local script. Ewarr1011 0 — 9y
View all comments (3 more)
0
Dude is it just supposed to be in workspace or what? because it's not working! Ewarr1011 0 — 9y
0
Localscripts have to be in the player, see here: http://wiki.roblox.com/index.php?title=API:Class/LocalScript Spongocardo 1991 — 9y
0
Is something wrong with roblox studio or is it the script? It's not working? Ewarr1011 0 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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)

Answer this question