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

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

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? :)

Here is the current script:

local Player = game.Players.LocalPlayer
    local Character = workspace:WaitForChild(Player.Name)
        local Humanoid = Character:WaitForChild('Humanoid')

local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = 'rbxassetid://2831460845' -- change id to the sprinting animation
RAnimation = Humanoid:LoadAnimation(RunAnimation)

Running = false

function Handler(BindName, InputState)
    if InputState == Enum.UserInputState.Begin and BindName == 'RunBind' then
        Character.Head.Running.PlaybackSpeed = 1.3 -- this makes the footsteps faster
        Running = true
        Humanoid.WalkSpeed = 25
    elseif InputState == Enum.UserInputState.End and BindName == 'RunBind' then
        Running = false
        Character.Head.Running.PlaybackSpeed = 1 -- this makes the footsteps back to the original state.
        if RAnimation.IsPlaying then
            RAnimation:Stop()
        end
        Humanoid.WalkSpeed = 16
    end
end

Humanoid.Running:connect(function(Speed)
    if Speed >= 10 and Running and not RAnimation.IsPlaying then
        RAnimation:Play()
        Humanoid.WalkSpeed = 25
    elseif Speed >= 10 and not Running and RAnimation.IsPlaying then
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
    elseif Speed < 10 and RAnimation.IsPlaying then
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
    end
end)

Humanoid.Changed:connect(function()
    if Humanoid.Jump and RAnimation.IsPlaying then
        RAnimation:Stop()
    end
end)

game:GetService('ContextActionService'):BindAction('RunBind', Handler, true, Enum.KeyCode.LeftShift) -- this makes it so the left shift make you sprint.

2 answers

You can use a BoolValue to reflect whether the player should be sprinting or not. When the hotkey is pressed all you gotta do is change the value. Tie this in with a function to change the WalkSpeed when the value changes.

BoolValue.Changed:Connect(function()
	if BoolValue.Value == true then
		Humanoid.WalkSpeed = 25
		BoolValue.Value = not BoolValue.Value -- changes the value between t/f
	elseif BoolValue.Value ~= true then -- if not true
		Humanoid.WalkSpeed = 16
		BoolValue.Value = not BoolValue.Value
	end
end

I have a different way of doing this. It goes like this:

game.Players.PlayerAdded:connect(function(p)
     p.Chatted:connect(function(msg)
         if msg:lower() == "sprint" then
               p.Character.Humanoid.WalkSpeed = 25
         end
         if msg:lower() == "unsprint" then
               p.Character.Humanoid.WalkSpeed = 16
         end
     end)
end)

Kinda like an admin command, without the admins.

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