Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Sprinting with UserInputService isn't working?

Asked by 6 years ago
Edited 6 years ago

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

0
Are there any errors? What happens that isn't meant to happen? iamnoamesa 674 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago

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!

0
Thanks a bunch! Loughdough 291 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

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)

Answer this question