Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Sprinting with UserInputService isn't working?

I'm a little bit of a noob to UserInputService. I don't think I've ever used anything other than the player's mouse. Anyways, I'm trying to make the player able to sprint. This script goes like this, placed in a local script inside StarterPlayerScripts:

local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild("Humanoid")

local defaultws = hum.WalkSpeed

local uis = game:GetService("UserInputService")

function onKeyPress(input)
	if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
		hum.WalkSpeed = 20
	end
end

function onKeyLeave(input)
	if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
		hum.WalkSpeed = defaultws
	end
end

uis.InputBegan:connect(onKeyPress)
uis.InputEnded:connect(onKeyLeave)

Any help is appreciated. Thanks! ~Loughdough

1 comment

0

Are there any errors? What happens that isn't meant to happen?

2 answers

✓ Accepted answer

Hi!

The issue with this script is that you attempt to index "char", which is a nil value since the Character doesn't exist yet. With player scripts like this, the script will run before the Character has loaded.

You are unable to use a WaitForChild() Instance since Character is a property of Player, not a child. So instead, you can use a Repeat Loop to wait until the Character has been loaded.

local player = game.Players.LocalPlayer
repeat wait() until player.Character --This will pause the script until the Character has successfully loaded.
local char = player.Character
local hum = char:WaitForChild("Humanoid")

local defaultws = hum.WalkSpeed

local uis = game:GetService("UserInputService")

function onKeyPress(input)
    if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
        hum.WalkSpeed = 20
    end
end

function onKeyLeave(input)
    if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
        hum.WalkSpeed = defaultws
    end
end

uis.InputBegan:connect(onKeyPress)
uis.InputEnded:connect(onKeyLeave)

Click "Accept Answer" if this was what you were looking for! Comment if you have any more concerns!

1 comment

0

Thanks a bunch!

To solve this problem, you need to make sure that .Character is not nil.

To do this, you can simply add a repeat loop to make sure that the character is in the game!

So in conclusion, your code should look like this:

local player = game.Players.LocalPlayer

repeat wait() until player.Character -- Makes sure that the character is in the game.

local char = player.Character
local hum = char:WaitForChild("Humanoid")

local defaultws = hum.WalkSpeed

local uis = game:GetService("UserInputService")

function onKeyPress(input)
    if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
        hum.WalkSpeed = 20
    end
end

function onKeyLeave(input)
    if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
        hum.WalkSpeed = defaultws
    end
end

uis.InputBegan:connect(onKeyPress)
uis.InputEnded:connect(onKeyLeave)

This discussion is preserved as it was. New answers, comments, and votes are closed.