Advanced Sprint Tutorial

the scripts work in both R6 and R15

#1 Sprint without stamina

Create a local script in StarterPlayerScript
image
The Script:

local uis = game:GetService("UserInputService")--User Input Service
local db = false --debounce
local tw = game:GetService("TweenService")--Tween service
local info = TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0) --tween info
local tween = tw:Create(workspace.CurrentCamera,info,{FieldOfView = 90}) --Starting Tween
local tweenBack = tw:Create(workspace.CurrentCamera,info,{FieldOfView = 70}) --Ending Tween

local SprintKey = Enum.KeyCode.LeftControl --Sprint Key

uis.InputBegan:Connect(function(key)--User Press Button
	if key.KeyCode == SprintKey then--Detect if player pressed spring Key
		db = true--set debounce to true
		while wait() do--loop
			if db == true and game.Players.LocalPlayer.Character then--detect if player have a character or not and if db = true
				repeat wait() until game.Players.LocalPlayer.Character.Humanoid--wait for humanoid
				game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 22--set walk speed to 22 "you can change that"
				tween:Play()--play the start tween
				break
			else
				break
			end
		end
	end	
end)

uis.InputEnded:Connect(function(key)--User Stop Pressing The Button
	if key.KeyCode == SprintKey then--Detect if player stoped holding pressed spring Key
		db = false--set debounce to false
		if game.Players.LocalPlayer.Character then--detect if player have a character
			repeat wait() until game.Players.LocalPlayer.Character.Humanoid--wait for humanoid
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16--set player speed to 16 "Normal Speed"
			tweenBack:Play()--Play the back tween
		end
	end
end)

#2 Sprint with stamina

First Step:

Create a local script in StarterPlayerScript
image
The Script:

local uis = game:GetService("UserInputService")--User Input Service
local db = false --debounce
local tw = game:GetService("TweenService")--Tween service
local info = TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0) --tween info
local tween = tw:Create(workspace.CurrentCamera,info,{FieldOfView = 90}) --Starting Tween
local tweenBack = tw:Create(workspace.CurrentCamera,info,{FieldOfView = 70}) --Ending Tween



local SprintKey = Enum.KeyCode.LeftControl --Sprint Key

uis.InputBegan:Connect(function(key)--User Press Button
	if key.KeyCode == SprintKey then--Detect if player pressed spring Key
		db = true--set debounce to true
		while wait() do--loop
			if db == true and game.Players.LocalPlayer.Character then--detect if player have a character or not and if db = true
				repeat wait() until game.Players.LocalPlayer.Character.Humanoid--wait for humanoid
				game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 22--set walk speed to 22 "you can change that"
				tween:Play()--play the start tween
				break
			else
				break
			end
		end
		while db == true do--detect if db == true
			if game.Players.LocalPlayer.Stamina.Value > 0 then--detect if player stamina > 0
				game.Players.LocalPlayer.Stamina.Value = game.Players.LocalPlayer.Stamina.Value - 1 --dicrease player stamina
			else
				db = false--if player stamina < of = to 0 db will set to false
				break
			end
			wait()
		end
		if db == false and game.Players.LocalPlayer.Stamina.Value < game.Players.LocalPlayer.MaxStamina.Value then--detect if db == false
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16--set player speed to normal speed "16"
			tweenBack:Play()--play back tween
		end
	end	
end)

uis.InputEnded:Connect(function(key)--User Stop Pressing The Button
	if key.KeyCode == SprintKey then--Detect if player stoped holding pressed spring Key
		db = false--set debounce to false
		if game.Players.LocalPlayer.Character then--detect if player have a character
			repeat wait() until game.Players.LocalPlayer.Character.Humanoid--wait for humanoid
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16--set player speed to 16 "Normal Speed"
			tweenBack:Play()--Play the back tween
			while db == false and game.Players.LocalPlayer.Stamina.Value < game.Players.LocalPlayer.MaxStamina.Value do
				wait()
				game.Players.LocalPlayer.Stamina.Value = game.Players.LocalPlayer.Stamina.Value + 1
			end
		end
	end
end)

Second Step:

I recommend you to check this Ui Scale and Size Tutorial or use this Plugin
Create new Gui
image

you can put the “StaminaBackFrame” To what ever you like

The “StaminaFrame” Size should be (1,0,1,0) and the position should be (0,0,0,0)


it should be a script inside a Ui elemant inside a back frame
in the local script past this script:

local StaminaBackFrame = script.Parent.Parent--get BackFrame
local StaminaFrame = script.Parent--get StaminaFrame

local Player = game.Players.LocalPlayer--get Player

local Stamina = Player:WaitForChild("Stamina")--get Player Stamina
local MaxStamina = Player:WaitForChild("MaxStamina")--get Player MaxStamina


local function refresh()--Function refresh
	script.Parent.Size = UDim2.new(Stamina.Value/MaxStamina.Value,0,1,0)--send stamina frame size
end

Stamina.Changed:Connect(function() --detect if Stamina changed
	refresh() --call the function
end)
MaxStamina.Changed:Connect(function()  --detect if MaxStamina changed
	refresh() --call the function
end)

Third Step:

Make a new Script in Server Script Service
image
Past This in:

game.Players.PlayerAdded:Connect(function(Player)--Detect if a player joined the gamed
	
	local MaxStamina = Instance.new("IntValue",Player)--add maxstamina to tha player Instance
	MaxStamina.Name = "MaxStamina"
	MaxStamina.Value = 50--Set the player max Stamina
	
	local Stamina = Instance.new("IntValue",Player)--add stamina to tha player Instance
	Stamina.Name = "Stamina"
	Stamina.Value = MaxStamina.Value--Make The player stamina = to maxstamina
	
end)

image

37 Likes

Wow this is good. Thank you so much!

1 Like

This helped so much, I have been wanting to do this for ages, ty!

1 Like

You should also include a “naruto” run animation in the tutorial. The player looks kinda bizarre when doing the same animation at a different speed.

1 Like

yeah ur right ppl love anime games this days

1 Like

no problem your welcom anytime

2 Likes

no problem your welcom anytime soon

2 Likes

This tutorial is pretty helpful. But you should’ve explained why you used what code on what part. And what it actually does and means.

4 Likes

The tutorial is pretty good. Just a few things, I wouldn’t recommend using the 2nd argument for Instance.new and you don’t need to wait for the humanoid to load for inputs most times.

1 Like

I don’t think that there is a purpose for this, when you can just use :WaitForChild and simplify your code and make it more efficient?

if key.KeyCode == SprintKey then--Detect if player pressed spring Key
    if db then -- since it's a boolean you can just do this
        local character = game.Players.LocalPlayer:WaitForChild("Character")
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.WalkSpeed = 22
        tween:Play()
    end
    -- continue code
1 Like

why not just connect the function directly to the event instead of a function calling the function?

Stamina.Changed:Connect(refresh) --detect if Stamina changed
MaxStamina.Changed:Connect(refresh)  --detect if MaxStamina changed
1 Like

uhh i have made comments in the script like

local Player = game.Player.LocalPlayer --getting the player inctance
1 Like

i think if i made the script work in starter player character it will be better

1 Like

yea sure i will use it in feature tutorials

1 Like

this causes problems i allways get an error bc of that and idk why but thx for the feedback

2 Likes

I’ve started using :BindAction() from the ContextActionService over UserInputService for binding inputs which will be used continuously throughout.

The Roblox Wiki does state that it’s better to use ContextActionService as it handles the context.

This is an issue as there’s no gameProcessedEvent and therefore there speed will change and stamina will expire even when not moving. Player movement is also controlled through ContextActionService so it would make more sense.

Apart from that, this is quite helpful.

1 Like

i can fix that by checking the Humanoid StateType ty for the feedback

1 Like

This is quite complex for no real reason…
Here’s something using ContextActionService which is far less complex

local TweenService = game:GetService("TweenService")
local Camera = workspace.CurrentCamera
local Humanoid = script.Parent:WaitForChild("Humanoid", 3) or error("Parent this to StarterCharacterScripts")

local FovIn = TweenService:Create(Camera, TweenInfo.new(1), {FieldOfView = 90})
local FovOut = TweenService:Create(Camera, TweenInfo.new(1), {FieldOfView = 70})

game:GetService("ContextActionService"):BindAction("Sprint", function(_, State)
    if State == Enum.UserInputState.Begin then
        Humanoid.WalkSpeed = 22

        FovIn:Play()
    else
        Humanoid.WalkSpeed = 16

        FovOut:Play()
    end
    -- is began? set it to 22. not? set it to 16
end, true, Enum.KeyCode.LeftControl)
3 Likes

thats cool but i am still not a pro scripter i dont know a lot of stuff about scripting and services, ty mate

2 Likes

I suggest improving the Cframe for the camera angle

1 Like