Why does this script work?

How does this script work? I’ve had it laying around for a while and never understood how/why it worked.

repeat wait() until game.Players.LocalPlayer
m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:connect(function(key)
	if key == "0" then 
		print("Running")
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 20
	end
end)
m.KeyUp:connect(function(key)
	if key == "0" then
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 14
	end
end)

What this script does is that it is a shift to sprint script, created by IronPhoenix_YT. As you can see, the script NEVER references shift.This was a LocalScript made to be in the StarterGUI.
EDIT: If shift is “0” then how do you find other key’s codes?

1 Like

If you print the key variable then when you press shift is says it is “0”, no idea why it prints that but i think thats how it functions

so i think when the key isnt a - z. it is a number instead

so 0 = shift

you can probably find other numbers of keycodes just by printing the key you press

1 Like

this is because you inputted “0”, referencing the number 0, which is not shift. you should change the key from 0 to “shift”.

1 Like

The issue was that it does work If you change it to “LeftShift” it doesn’t work.

In your case left shift is "304"
find more about key code numbers here: KeyCode | Roblox Creator Documentation

2 Likes

But if you change the “0” to “304” it doesn’t work anymore. Also, why did 0 ever work in the first place?

the 0 value is unknown input

you can use user input service instead, like in the example below

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input, gameProcessed)
	--if gameProcessed then return end --/// if you are using keycodes like shift or others (esc, left shift, etc.) you can delete this, but it will also fire if player is typing
	--
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			print("left shift pressed!")
		end
	end
end)

put it into a local script (in starter gui or starter player scripts)

for holding and not holding use

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input, gameProcessed)
	--if gameProcessed then return end --/// if you are using keycodes like shift or others (esc, left shift, etc.) you can delete this, but it will also fire if player is typing
	--
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			print("left shift is now being holded")
		end
	end
end)

uis.InputEnded:Connect(function(input, gameProcessed)
	--if gameProcessed then return end --/// if you are using keycodes like shift or others (esc, left shift, etc.) you can delete this, but it will also fire if player is typing
	--
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			print("left shift is now not being holded")
		end
	end
end)
1 Like