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

How do I make it so you don't have to hold the key to sprint in this script?

Asked by 5 years ago

This current script makes it so when you HOLD down the shift key you sprint, however I'd like to make it so when you press the key you can sprint but you don't need to hold the key, and then once you press the key again, you stop sprinting? How would I do this? Your help would be greatly appreciated! :)

wait()local Player=game.Players.LocalPlayer local Mouse = Player:GetMouse() local Speed = 33 local Humanoid = Player.Character:WaitForChild("Humanoid") local OrigSpeed = Humanoid.WalkSpeed

Mouse.KeyDown:connect(function(Key)local Code=Key:byte()if(Code==48)then Humanoid.WalkSpeed = Speed end end) Mouse.KeyUp:connect(function(Key)local Code=Key:byte()if(Code==48)then Humanoid.WalkSpeed = OrigSpeed end end)

1 answer

Log in to vote
1
Answered by 5 years ago

I set the button to activate it as shift but you can change that to whatever you want. Don't use KeyDown anymore, it's deprecated, UserInputService is more efficient.

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
Player.CharacterAdded:wait()

local Mouse = Player:GetMouse()
local Speed = 33
local Humanoid = Player.Character:WaitForChild("Humanoid")
local OrigSpeed = Humanoid.WalkSpeed
local active = false

UserInputService.InputBegan:Connect(function(input)
local key = input.KeyCode
    if key == Enum.KeyCode.LeftShift and active == false then
        Humanoid.WalkSpeed = Speed
        active = true
    elseif key == Enum.KeyCode.LeftShift and active == true then
        Humanoid.WalkSpeed = OrigSpeed
        active = false
    end
end) 
Ad

Answer this question