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

LeftShift to sprint script doesn't work ?

Asked by 6 years ago

I made a little script that should change the player's walkspeed to 50 when LeftShift is pressed but it didn't work.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == Enum.KeyCode.LeftShift then
        local Human = script.Parent:WaitForChild("Humanoid")
        Human.Walkspeed = 50
end
    if not key then
        local Human = script.Parent:WaitForChild("Humanoid")
        Human.Walkspeed = 16
    end
end)

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This already won't work because you're using 'KeyDown' wich is deprecated and will not work anymore, use userinputservice instead.

Not sure where your script is but most of the time player isn't the parent, (mostly playergui or backpack)

Also, you're thinking humanoid is located inside the player wich is not the case, it's located inside the character

So a better script would be

local UserInputService = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer

function onInputBegan(input,gameProcessed)
        if input.KeyCode == Enum.KeyCode.LeftShift then
        local char = Player.Character
        if char then
            local h = char:WaitForChild('Humanoid')
            h.WalkSpeed = 50
        end
    end
end

function onInputEnded(input,gameProcessed)
     if input.KeyCode == Enum.KeyCode.LeftShift then
        local char = Player.Character
        if char then
            local h = char:WaitForChild('Humanoid')
            h.WalkSpeed = 16
        end
    end
end


UserInputService.InputBegan:connect(onInputBegan)
UserInputService.InputEnded:connect(onInputEnded)

This needs to be a local script in any client area.

If you have any questions, just ask :)

0
Thanks ! Worked xJathur95x 129 — 6y
0
Np :) User#20388 0 — 6y
Ad
Log in to vote
0
Answered by
TheePBHST 154
6 years ago
Edited 6 years ago

Right, so a mouse. Does it have keys? No a keyboard does. So, instead of using the mouse. You should use something called UserInputService.

So, just do..

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(key_) -- Why the underscore? Because thats the keycode of the key.
    local key = key_.KeyCode
    if key == Enum.KeyCode.YourKeyHere then
        --code
    end
end)
0
He was using keydown lol User#20388 0 — 6y

Answer this question