My walk and sprint code not working

Problem: I for a game I am making I am try to add a run walk system in a Normal Script in StarterCharacterScripts but it isn’t working.

I Have tried to changing the if Statements to

if Input == Enum.KeyCode.--Key would go here

Code:

--getting UserInputService
local UserInputService = game:GetService("UserInputService")

--Key pressed event
UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		script.Parent.Humanoid.WalkSpeed = 100
	elseif Input == Enum.KeyCode.LeftControl then
		script.Parent.Humanoid.WalkSpeed = 1
	end
end)

--Key let go event
UserInputService.InputEnded:Connect(function(Input, GameProcessedEvent)
	if Input.KeyCode == Enum.KeyCode.LeftShift or Input.KeyCode == Enum.KeyCode.LeftControl then
		script.Parent.Humanoid.WalkSpeed = 16
	end
end)

Thanks for you help :grinning:

You need to switch to a LocalScript. UserInputService is handled on the client-side. All things client should be used in a LocalScript.

Your code otherwise looks fine.

5 Likes

I tried the code I posted in Studio and it works fine just do that

This code when placed inside a LocalScript in StarterPlayer in StarterCharacterScripts

Your code has to be in a local script. I would recommend putting it inside playerscripts. (search for that in workspace). Regular scripts cannot use UserInputService

Set it up like this:
image
Server Script will not work.