I’m trying to make a custom key feature for movements such as sprint, but when I try to bind sprint with modifier keys such as LeftShift or LeftControl it sets the keybind to nil?
local c = script.Parent.Crouch.TextBox
local s = game.ReplicatedStorage.MovementHandler.Keybinds
local UIS = game:GetService("UserInputService")
c.Activated:Connect(function()
listeningForSprintInput = true
c.Text = "?"
end)
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if listeningForSprintInput == true then
local sprintSelected = UIS:GetStringForKeyCode(input.KeyCode)
c.Text = sprintSelected
s.Crouch.Value = sprintSelected
c.Text = s.Crouch.Value
listeningForSprintInput = false
end
end
end)
Activating the text box might be overriding some keys, like shift for uppercase. You should use a button instead, and do something like this:
local c = script.Parent.Crouch.TextButton
local s = game.ReplicatedStorage.MovementHandler.Keybinds
local inputConnection
local listening = false
function disc()
inputConnection:Disconnect()
end
c.MouseButton1Click:Connect(function()
if listening then return end --if already listening for input, dont do
inputConnection = UIS.InputBegan:Connect(function(input) --set the connection and listen
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
--player pressed a button
local sprintSelected = UIS:GetStringForKeyCode(input.KeyCode)
c.Text = sprintSelected
s.Crouch.Value = sprintSelected
c.Text = s.Crouch.Value
listening = false --no longer listening since the keybind was set
disc() --disconnect the input connection because no longer listening for connection
end)
end)
May not affect anything, but try changing input to something else.
i.e.
local c = script.Parent.Crouch.TextBox
local s = game.ReplicatedStorage.MovementHandler.Keybinds
local UIS = game:GetService("UserInputService")
c.Activated:Connect(function()
listeningForSprintInput = true
c.Text = "?"
end)
UIS.InputBegan:Connect(function(key)
if key.UserInputType == Enum.UserInputType.Keyboard then
if listeningForSprintInput == true then
local sprintSelected = UIS:GetStringForKeyCode( key.KeyCode)
c.Text = sprintSelected
s.Crouch.Value = sprintSelected
c.Text = s.Crouch.Value
listeningForSprintInput = false
end
end
end)
I think the only problem is that UIS:GetStringForKeyCode just doesn’t work for shift. If you print (input.KeyCode), it detects shift. Try this, it seems to work fine:
local sprintSelected,_ = string.gsub(tostring(input.KeyCode),"Enum.KeyCode.","")
c.Text = sprintSelected
--displays "LeftShift" if shift was pressed
(also if you want it to work even when shiftlock is enabled, use InputEnded instead of InputBegan)