What do you want to achieve? I’m making a poses\animations GUI. When the player presses one of the buttons, the corresponding animation plays. So far, so good… I also wanted to add customizable keybindings where the player can input whatever letter or number they want in the matching textbox and by pressing that same key, the animation will play.
What is the issue? When trying to index the textbox’s Text with the UserInputType (input.UserInputType == Enum.KeyCode.kb) the console gives me this error:
Here is the full code in a LocalScript:
local id = script.Parent.Animation
local plr = game.Players.LocalPlayer
local kb = script.Parent.keybind
local UIS = game:GetService("UserInputService")
--Play animation function
local function playAnim()
local char = plr.Character
local humanoid = char:FindFirstChildOfClass("Humanoid")
if humanoid then
local animator = humanoid.Animator
if animator then
local animationTrack = animator:LoadAnimation(id)
animationTrack:Play()
while animationTrack.IsPlaying do
wait(0.1)
char.HumanoidRootPart.Anchored = true
end
char.HumanoidRootPart.Anchored = false
return animationTrack
end
end
end
--Character cap in keybind TextBox
kb.Changed:connect(function()
kb.Text = kb.Text:sub(1,1)
end)
--Custom keybind (that's where the error is)
local function keybind(input)
if input.UserInputType == Enum.KeyCode.kb.Text then
playAnim()
end
end
--Triggers
UIS.InputBegan:Connect(keybind)
script.Parent.Activated:Connect(playAnim)
And here is the hierarchy, all in StarterGui of course:
What solutions have you tried so far?
After some attempts, I realized it’s impossible to index something to Enum.Keycode unless it’s the code of a key. The issue basically is to convert the .Text of the textbox into the code and then indexing it to Enum.Keycode.
I tried searching in both the Wiki and the Forum, nothing seemed to help.
What do you mean exactly?
If I understand your question correctly, the keybind can be decided by the user by inputting the letter or number they want in the TextBox. ^^
That’s cool to know, but I don’t understand how that could help… ^^"
assuming I manage to find a way to implement your method, I’d have to do something similar for every letter and number, which is quite tedious.
Am I missing something? It seems like the script isn’t “logging” the Textbox’s Text now.
--Custom keybind
local function keybind(input)
if Enum.KeyCode[kb.Text:upper()] and input.UserInputType == Enum.KeyCode[kb.Text:upper()] then
playAnim()
end
end
Oh, that makes the code a little bit neater, thank you.
I just noticed this, but change the input.UserInputType to input.KeyCode sometimes it messes the code up. But here is the fixed script in whole if needed:
local plr = game.Players.LocalPlayer
local kb = script.Parent.keybind
local UIS = game:GetService("UserInputService")
local function playAnim()
local char = plr.Character
local humanoid = char:FindFirstChildOfClass("Humanoid")
if humanoid then
local animator = humanoid.Animator
if animator then
local animationTrack = animator:LoadAnimation(id)
animationTrack:Play()
while animationTrack.IsPlaying do
wait(0.1)
char.HumanoidRootPart.Anchored = true
end
char.HumanoidRootPart.Anchored = false
return animationTrack
end
end
end
kb.Changed:connect(function()
kb.Text = kb.Text:sub(1,1):upper()
end)
--Custom keybind (that's where the error is)
local function keybind(input)
if string.len(kb.Text) > 0 then
if Enum.KeyCode[kb.Text] and input.KeyCode == Enum.KeyCode[kb.Text] then
playAnim()
end
end
end
--Triggers
UIS.InputBegan:Connect(keybind)
script.Parent.Activated:Connect(playAnim)
Thank you so much! That works wonderfully. I was also trying to prevent errors if the Text was nil but you already did it, thank you once again! One last question, is there a way to prevent the keybind from being triggered while typing in chat? Apologies if I ask so many questions.