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

How can I detect the pressed key?

Please help me! The output says " bad argument #2 to '?' (string expected, got Object) "

local plr = game:GetService("Players")
local mouse = plr:GetMouse()
local text = script.Parent.TextLabel

mouse.KeyDown:connect(function(key)
   text.Text = key
end

2 answers

✓ Accepted answer

This is because the Mouse.KeyDown event is deprecated. Instead, use the UserInputService.

local text = script.Parent.TextLabel

game:GetService("UserInputService").InputBegan:Connect(function(input, chatting)
    text.Text = tostring(input.KeyCode)
end)

4 comments

0

thank you!

0

You can use input.KeyCode.Name

0

Okay!

0

Yeah^^

That's because you forgot the LocalPlayer after game:GetService("Players")

local plr = game:GetService("Players").LocalPlayer --here
local mouse = plr:GetMouse()
local text = script.Parent.TextLabel

mouse.KeyDown:Connect(function(key) --use uppercase Connect
   text.Text = key
end

Although as incapaz mentioned, you should use UserInputService instead.

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