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

How can I detect the pressed key?

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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)
0
thank you! 0_Halloween 166 — 6y
0
You can use input.KeyCode.Name User#5423 17 — 6y
0
Okay! 0_Halloween 166 — 6y
0
Yeah^^ User#19524 175 — 6y
Ad
Log in to vote
2
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

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.

Answer this question