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

LocalScript in StarterCharacterScript is not working?

local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = Char:WaitForChild("Humanoid")

local UserInputService = game:GetService("UserInputService")
 
UserInputService.InputBegan:Connect(function(input)
    local keyPressed = input.KeyCode
        if keyPressed == Enum.KeyCode.LeftShift then -- put key in parenthesis
                Char.Humanoid.WalkSpeed = 32
        end
end)
 
UserInputService.InputEnded:Connect(function(input)
    local keyReleased = input.KeyCode
        if keyReleased == Enum.KeyCode.LeftShift then -- put key in parenthesis
                Char.Humanoid.WalkSpeed = 16
        end
end)

Why is this not working?

2 comments

0

functions not closed? no?

0

What function

1 answer

✓ Accepted answer

You are directing the KeyCode without the Enum being used. Fixed script:

local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
humanoid = Char:WaitForChild("Humanoid") -- humanoid should be global not local

UserInputService = game:GetService("UserInputService") -- also should be global

UserInputService.InputBegan:Connect(function()
        if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) then -- use IsKeyDown()
                humanoid.WalkSpeed = 32
        end
end)

UserInputService.InputEnded:Connect(function()
                humanoid.WalkSpeed = 16
end)

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