Keycode blank space sometimes when assigned to a string value

So I’m making a sprint script that uses the key to sprint that the player has manually assigned with their keyboard, and when I press left shift (default keycode), for some reason, it shows as a blank space in the sprinting script error?
image
The setting button code:

local button = script.Parent
local UIS = game:GetService('UserInputService')
local canyaclick = true

button.MouseButton1Down:Connect(function()
	if canyaclick then
		canyaclick = false
		lol = UIS.InputBegan:Connect(function(i,g)
			if not g and i.KeyCode then
				lol:Disconnect()
				local lekey = UIS:GetStringForKeyCode(i.KeyCode)
				button.Text = "sprint key; ".. lekey
				print("key-"..lekey)
				script.Parent.Parent.Parent.Parent.Noreset.SprintKey.Value = UIS:GetStringForKeyCode(i.KeyCode)
				canyaclick = true
			end
		end)
	end
end)

This is really infuriating because every other keycode works… what’d I do wrong?

It seems to be a problem with GetStringForKeyCode, I think it’s only meant to handle letters and not special keys like shift and enter. You can replace it with i.KeyCode.Name.

1 Like

Keycodes don’t have the property “Name” on the developer hub though?

Edit: People are saying it works, but I get the same error with it.

local button = script.Parent
local UIS = game:GetService('UserInputService')
local canyaclick = true

button.MouseButton1Down:Connect(function()
	if canyaclick then
		canyaclick = false
		lol = UIS.InputBegan:Connect(function(i,g)
			if not g then return end
			local lekey = UIS:GetStringForKeyCode(i.KeyCode)
			button.Text = "sprint key; ".. lekey
			print("key-"..lekey)
			script.Parent.Parent.Parent.Parent.Noreset.SprintKey.Value = UIS:GetStringForKeyCode(i.KeyCode)
			canyaclick = true
		end)
	end
end)

I think the not is accounting for the keycode too.

They do

Can you send you code? I didn’t get any errors.

No, it gets past that.

Also @Judgy_Oreo here’s my code:

local button = script.Parent
local UIS = game:GetService('UserInputService')
local canyaclick = true

button.MouseButton1Down:Connect(function()
	if canyaclick then
		canyaclick = false
		lol = UIS.InputBegan:Connect(function(i,g)
			if not g and i.KeyCode then
				lol:Disconnect()
				local lekey = UIS:GetStringForKeyCode(i.KeyCode)
				lekey = lekey.Name
				button.Text = "sprint key; ".. lekey
				print("key-"..lekey)
				script.Parent.Parent.Parent.Parent.Noreset.SprintKey.Value = lekey
				canyaclick = true
			end
		end)
	end
end)

Gotta head off so I’ll talk tomorrow though

One more question why r u disconnecting the function?

It doesn’t look like the error comes from the code you posted, Enum.KeyCode is never accessed.

Going to take a guess and say that you might doing something like this in another script to check if the sprint key is being pressed:

-- Noreset.SprintKey.Value is set on line 14 of the code provided to "" (an empty string)
if input.KeyCode == Enum.KeyCode[Noreset.SprintKey.Value] then
    --- Sprint
end

Which is what’s actually causing the error. @Judgy_Oreo’s suggestion of using the Enum’s name instead is probably the best solution.

1 Like

You aren’t using i.KeyCode.Name anywhere in the code, so the error will still be there if you didn’t change anything.

If I didn’t I think it would fire multiple times.