Making minecraft sprint

Hello, I would like to make a sprinting system similar to the game “Minecraft” but instead of double tapping just the W key I want it for WASD to sprint instead of just W, I also want to add a tiny cooldown so you can’t press W and A at the same time, ect.

Not the full solution or best but something

local u = game:GetService("UserInputService")
local x = 0
u.InputBegan:Connect(function(k)
	if k.KeyCode == Enum.KeyCode.W then
		x+=1
		if x == 2 then
			--do stuff
		end
	end
end)

I’ve made a post before, I’ve tested this, and works very well, Also

this is a LocalScript from StarterCharacterScripts

1 Like

Oh, I didn’t see your post, sorry.

How exactly would I add a limit to how far they can run?

Do you mean the sprint walkspeed?

You can change it on line 16

local SprintingWalkSpeed = 20

No, like if I wanted a bar, and if that bar runs out (which it slowly runs out while running) if hit 0 then you cannot run until it recharges.

repeat task.wait() until game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

local UserInputService = game:GetService("UserInputService")
local StarterPlayer = game:GetService("StarterPlayer")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local DefaultFieldOfView = 70
local DefaultWalkSpeed = 16

local SprintingFieldOfView = DefaultFieldOfView * 1.25
local SprintingWalkSpeed = DefaultWalkSpeed * 1.5

local Stamina = 100
local Sprinting = false

local TweeningTime = 0.3

local LastTaps = {
	W = tick(),
	A = tick(),
	S = tick(),
	D = tick()
}

Camera.FieldOfView = DefaultFieldOfView
Humanoid.WalkSpeed = DefaultWalkSpeed

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W  or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		if LastTaps[Input.KeyCode.Name] and Stamina > 0 then
			local TimeDistance = tick() - LastTaps[Input.KeyCode.Name]

			LastTaps[Input.KeyCode.Name] = tick()

			if TimeDistance <= 0.2 and not Sprinting then
				Sprinting = true

				TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = SprintingFieldOfView}):Play()
				Humanoid.WalkSpeed = SprintingWalkSpeed
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W  or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		if Sprinting then
			Sprinting = false

			TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
			Humanoid.WalkSpeed = DefaultWalkSpeed
		end
	end
end)

RunService.RenderStepped:Connect(function()
	if Sprinting then
		Stamina -= 0.25
	else
		Stamina += 0.25
	end

	Stamina = math.clamp(Stamina, 0, 100)
	
	print("Player has " .. math.floor(Stamina) .. " stamina")
	
	if Stamina <= 0 and Sprinting ~= false then
		Sprinting = false

		TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
		Humanoid.WalkSpeed = DefaultWalkSpeed
	end
end)

Oh yeah, one last thing, if you’re sprinting using W and switch to A you stop sprinting, any fixes?

fixed

repeat task.wait() until game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

local UserInputService = game:GetService("UserInputService")
local StarterPlayer = game:GetService("StarterPlayer")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local DefaultFieldOfView = 70
local DefaultWalkSpeed = 16

local SprintingFieldOfView = DefaultFieldOfView * 1.25
local SprintingWalkSpeed = DefaultWalkSpeed * 1.5

local Stamina = 100
local StartKeyCode = nil
local Sprinting = false

local TweeningTime = 0.3

local LastTaps = {
	W = tick(),
	A = tick(),
	S = tick(),
	D = tick()
}

Camera.FieldOfView = DefaultFieldOfView
Humanoid.WalkSpeed = DefaultWalkSpeed

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W  or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		if LastTaps[Input.KeyCode.Name] then
			local TimeDistance = tick() - LastTaps[Input.KeyCode.Name]
			
			LastTaps[Input.KeyCode.Name] = tick()

			if TimeDistance <= 0.2 and not Sprinting then
				Sprinting = true
				
				StartKeyCode = Input.KeyCode

				TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = SprintingFieldOfView}):Play()
				Humanoid.WalkSpeed = SprintingWalkSpeed
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W  or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		if Sprinting and StartKeyCode == Input.KeyCode then
			Sprinting = false

			TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
			Humanoid.WalkSpeed = DefaultWalkSpeed
		end
	end
end)

RunService.RenderStepped:Connect(function()
	if Sprinting then
		Stamina -= 0.25
	else
		Stamina += 0.25
	end

	Stamina = math.clamp(Stamina, 0, 100)

	if Stamina <= 0 and Sprinting ~= false then
		Sprinting = false

		TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
		Humanoid.WalkSpeed = DefaultWalkSpeed
	end
end)

RunService.RenderStepped:Connect(function()
	if Sprinting then
		Stamina -= 0.25
	else
		Stamina += 0.25
	end

	Stamina = math.clamp(Stamina, 0, 100)

	if Stamina <= 0 and Sprinting ~= false then
		Sprinting = false

		TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
		Humanoid.WalkSpeed = DefaultWalkSpeed
	end
end)

You can’t make a hard turn / sharp turn without moving your camera.

repeat task.wait() until game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

local UserInputService = game:GetService("UserInputService")
local StarterPlayer = game:GetService("StarterPlayer")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local DefaultFieldOfView = 70
local DefaultWalkSpeed = 16

local SprintingFieldOfView = DefaultFieldOfView * 1.25
local SprintingWalkSpeed = DefaultWalkSpeed * 1.5

local Stamina = 100
local StartKeyCode = nil
local Sprinting = false

local TweeningTime = 0.3

local LastTaps = {
	W = tick(),
	A = tick(),
	S = tick(),
	D = tick()
}

Camera.FieldOfView = DefaultFieldOfView
Humanoid.WalkSpeed = DefaultWalkSpeed

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W  or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		if LastTaps[Input.KeyCode.Name] then
			local TimeDistance = tick() - LastTaps[Input.KeyCode.Name]
			
			LastTaps[Input.KeyCode.Name] = tick()

			if TimeDistance <= 0.2 and not Sprinting then
				Sprinting = true
				
				StartKeyCode = Input.KeyCode

				TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = SprintingFieldOfView}):Play()
				Humanoid.WalkSpeed = SprintingWalkSpeed
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W  or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		if Sprinting and StartKeyCode == Input.KeyCode then
			Sprinting = false

			TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
			Humanoid.WalkSpeed = DefaultWalkSpeed
		end
	end
end)

RunService.RenderStepped:Connect(function()
	if Sprinting then
		Stamina -= 0.25
	else
		Stamina += 0.25
	end

	Stamina = math.clamp(Stamina, 0, 100)

	if Stamina <= 0 and Sprinting ~= false then
		Sprinting = false

		TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
		Humanoid.WalkSpeed = DefaultWalkSpeed
	end
end)

I accidentally put the Render Step thingy 2 times

It didn’t fix if you make a hard turn / sharp turn without moving your camera. For example, if you hold W and then A and then stop holding W it stops you from running.

repeat task.wait() until game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

local UserInputService = game:GetService("UserInputService")
local StarterPlayer = game:GetService("StarterPlayer")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local DefaultFieldOfView = 70
local DefaultWalkSpeed = 16

local SprintingFieldOfView = DefaultFieldOfView * 1.25
local SprintingWalkSpeed = DefaultWalkSpeed * 1.5

local Stamina = 100
local Sprinting = false

local TweeningTime = 0.3

local LastTaps = {
	W = tick(),
	A = tick(),
	S = tick(),
	D = tick()
}

local HoldingKeys = {}

Camera.FieldOfView = DefaultFieldOfView
Humanoid.WalkSpeed = DefaultWalkSpeed

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W  or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		if LastTaps[Input.KeyCode.Name] then
			local TimeDistance = tick() - LastTaps[Input.KeyCode.Name]

			LastTaps[Input.KeyCode.Name] = tick()

			if TimeDistance <= 0.2 and not Sprinting then
				Sprinting = true

				TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = SprintingFieldOfView}):Play()
				Humanoid.WalkSpeed = SprintingWalkSpeed
			end
		end
	end
	
	if not table.find(HoldingKeys, Input.KeyCode.Name) then
		table.insert(HoldingKeys, Input.KeyCode.Name)
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if table.find(HoldingKeys, Input.KeyCode.Name) then
		table.remove(HoldingKeys, table.find(HoldingKeys, Input.KeyCode.Name))
	end
	
	if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		local KeepSprinting = false
		
		for Keycode, Value in pairs(LastTaps) do
			if table.find(HoldingKeys, Keycode) then
				KeepSprinting = true
				
				break
			end
		end
		
		if Sprinting and KeepSprinting == false then
			Sprinting = false

			TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
			Humanoid.WalkSpeed = DefaultWalkSpeed
		end
	end
end)

RunService.RenderStepped:Connect(function()
	if Sprinting then
		Stamina -= 0.25
	else
		Stamina += 0.25
	end

	Stamina = math.clamp(Stamina, 0, 100)

	if Stamina <= 0 and Sprinting ~= false then
		Sprinting = false

		TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
		Humanoid.WalkSpeed = DefaultWalkSpeed
	end
end)

finally fixed

3 Likes

Very sorry to bother 4 days later, but I have one last question, how exactly would I make it so there’s a cooldown before they regain stamina?

repeat task.wait() until game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

local UserInputService = game:GetService("UserInputService")
local StarterPlayer = game:GetService("StarterPlayer")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local DefaultFieldOfView = 70
local DefaultWalkSpeed = 16

local SprintingFieldOfView = DefaultFieldOfView * 1.25
local SprintingWalkSpeed = DefaultWalkSpeed * 1.5

local SprintCooldown = 0
local MaxStamina = 100
local MinStamina = 0
local Stamina = MaxStamina
local CanSprint = true
local Sprinting = false

local TweeningTime = 0.3

local LastTaps = {
	W = tick(),
	A = tick(),
	S = tick(),
	D = tick()
}

local HoldingKeys = {}

Camera.FieldOfView = DefaultFieldOfView
Humanoid.WalkSpeed = DefaultWalkSpeed

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W  or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		if LastTaps[Input.KeyCode.Name] then
			local TimeDistance = tick() - LastTaps[Input.KeyCode.Name]

			LastTaps[Input.KeyCode.Name] = tick()

			if TimeDistance <= 0.2 and not Sprinting and CanSprint then
				Sprinting = true

				TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = SprintingFieldOfView}):Play()
				Humanoid.WalkSpeed = SprintingWalkSpeed
			end
		end
	end

	if not table.find(HoldingKeys, Input.KeyCode.Name) then
		table.insert(HoldingKeys, Input.KeyCode.Name)
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if table.find(HoldingKeys, Input.KeyCode.Name) then
		table.remove(HoldingKeys, table.find(HoldingKeys, Input.KeyCode.Name))
	end

	if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		local KeepSprinting = false

		for Keycode, Value in pairs(LastTaps) do
			if table.find(HoldingKeys, Keycode) then
				KeepSprinting = true

				break
			end
		end

		if Sprinting and KeepSprinting == false then
			Sprinting = false
			CanSprint = false

			TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
			Humanoid.WalkSpeed = DefaultWalkSpeed
		end
	end
end)

RunService.RenderStepped:Connect(function()
	if Sprinting then
		Stamina = math.clamp(Stamina - 0.25, MinStamina, MaxStamina)
	else
		Stamina = math.clamp(Stamina + 0.25, MinStamina, MaxStamina)
	end
	
	if Stamina >= MaxStamina and not CanSprint then
		coroutine.wrap(function()
			task.wait(SprintCooldown)

			CanSprint = true
		end)()
	end

	if Stamina <= 0 and Sprinting ~= false then
		Sprinting = false

		TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
		Humanoid.WalkSpeed = DefaultWalkSpeed
	end
end)

I didn’t mean a cooldown to stop them from sprinting, I wanted a cooldown to stop stamina from regaining and then start after a period of time gaining.

Hello, are you there? It’s been 3 days. What I basically want is a cooldown so it doesn’t regenerate the stamina instantly when you stop running.

I wanna try and get the game out as fast as possible, and I am trying to make it myself but it doesn’t work each time, how would I make it so there’s a cooldown until they start sprinting?

repeat task.wait() until game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

local UserInputService = game:GetService("UserInputService")
local StarterPlayer = game:GetService("StarterPlayer")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local DefaultFieldOfView = 70
local DefaultWalkSpeed = 16

local SprintingFieldOfView = DefaultFieldOfView * 1.25
local SprintingWalkSpeed = DefaultWalkSpeed * 1.5

local Stamina = 100
local Sprinting = false

local TweeningTime = 0.3
local LastSprintedOn = tick()

local LastTaps = {
	W = tick(),
	A = tick(),
	S = tick(),
	D = tick()
}

local HoldingKeys = {}

Camera.FieldOfView = DefaultFieldOfView
Humanoid.WalkSpeed = DefaultWalkSpeed

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W  or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		if LastTaps[Input.KeyCode.Name] then
			local TimeDistance = tick() - LastTaps[Input.KeyCode.Name]

			LastTaps[Input.KeyCode.Name] = tick()

			if TimeDistance <= 0.2 and not Sprinting then
				Sprinting = true

				TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = SprintingFieldOfView}):Play()
				Humanoid.WalkSpeed = SprintingWalkSpeed
			end
		end
	end

	if not table.find(HoldingKeys, Input.KeyCode.Name) then
		table.insert(HoldingKeys, Input.KeyCode.Name)
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if table.find(HoldingKeys, Input.KeyCode.Name) then
		table.remove(HoldingKeys, table.find(HoldingKeys, Input.KeyCode.Name))
	end

	if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
		local KeepSprinting = false

		for Keycode, Value in pairs(LastTaps) do
			if table.find(HoldingKeys, Keycode) then
				KeepSprinting = true

				break
			end
		end

		if Sprinting and KeepSprinting == false then
			Sprinting = false
			
			TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
			Humanoid.WalkSpeed = DefaultWalkSpeed
			
			LastSprintedOn = tick()
		end
	end
end)

RunService.RenderStepped:Connect(function()
	if Sprinting then
		Stamina = math.clamp(Stamina - 0.25, 0, 100)
	else
		if tick() - LastSprintedOn > 0.25 then
			Stamina = math.clamp(Stamina + 0.25, 0, 100)
		end
	end

	if Stamina <= 0 and Sprinting ~= false then
		Sprinting = false

		TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
		Humanoid.WalkSpeed = DefaultWalkSpeed
	end
end)