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

Sprinting script not working even though I followed an answer on an older post?

Asked by 3 years ago

So, I recently got the motivation and started working on a new game I'm making. I want it to have a sprinting feature, but I can't seem to figure out what is the problem here.

I also followed a tutorial, YouTube link here: https://youtu.be/4z1HVwe0sLE

So I looked in another post in scriptinghelpers, and I found this; https://scriptinghelpers.org/questions/81021/hey-how-do-i-get-character-from-player-in-a-local-script

I followed the answer that someone posted and put it in my variables, but it still wouldn't work.

Here is the code snippet:

UserInputService = game:GetService("UserInputService")
local LShiftPressed = false
local Player = game.Players.LocalPlayer.Character
local Humanoid = Player.Humanoid

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        LShiftPressed = true

        while LShiftPressed == true do
            wait()
            Humanoid.WalkSpeed = 32
            game.Workspace.Sound.Sprint:Play()
        end

    end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        LShiftPressed = false

        while LShiftPressed == false do
            wait()
            Humanoid.WalkSpeed = 16
        end

    end
end)

Thank you if you have any feedback. Sorry if this code appears messy.

2 answers

Log in to vote
0
Answered by 3 years ago

You're indexing character, so there's that. It could potentially be the Humanoid's name, or that the Sound Instance in workspace is not available or not created.

I would recommend using methods such as Instance.WaitForChild, Instance.FindFirstChildWhichIsA to work around for a fix.

0
I tried WaitForChild() and FindFirstChildWhichIsA() on the "Humanoid" variable but it still didn't work. KneeDeepInTheDoot 13 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

try

UserInputService = game:GetService("UserInputService")
local LShiftPressed = false

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        LShiftPressed = true
        print(LShiftPressed)

        while LShiftPressed == true do
            wait()
            game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 32
            game.Workspace.Sound.Sprint:Play()
        end

    end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        LShiftPressed = false
        print(LShiftPressed)

        while LShiftPressed == false do
            wait()
            game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 16
        end

    end
end)

if it doesn't work, dm me on discord

Blue Duck#8344

0
if it still doesn't work, turn off shiftlock by clicking starterplayer and at the properties find EnableMouseLock and turn it off WINDOWS10XPRO 438 — 3y

Answer this question