Sprinting system, any room for code improvement or cleanup?

Pretty much a sprinting system that detects when you hold down the Left Shift button.
If you’re not moving and still holding down the Left Shift button, will disable the effects and slow you down again.

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui

local MainGui = PlayerGui:WaitForChild("MainGui")
local SprintingVignette = MainGui:FindFirstChild("SprintingVignette")

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Camera = workspace.Camera

local defaultSpeed = 16
local sprintingSpeed = defaultSpeed * 1.5

local Animation = Instance.new("Animation", Character)
Animation.Name = "SprintAnimation"
Animation.AnimationId = "rbxassetid://6285133880"
local SprintAnimation = Character.Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(Animation)

local function Disable()
	TweenService:Create(Camera, TweenInfo.new(1), {FieldOfView = 70}):Play()
	TweenService:Create(SprintingVignette, TweenInfo.new(1), {ImageTransparency = 1}):Play()
	SprintAnimation:Stop()
	Humanoid.WalkSpeed = defaultSpeed
end

local function EnableSprinting(key, gameProcessed)
	if gameProcessed then
		Disable()
		return 
	end
	if key.KeyCode == Enum.KeyCode.LeftShift and (Humanoid.MoveDirection.Magnitude > 0) then
		TweenService:Create(Camera, TweenInfo.new(1), {FieldOfView = 80}):Play()
		TweenService:Create(SprintingVignette, TweenInfo.new(1), {ImageTransparency = 0.5}):Play()
		SprintAnimation:Play()
		Humanoid.WalkSpeed = sprintingSpeed
		SprintAnimation:AdjustSpeed(1.35)
	end
end

local function DisableSprinting(key, gameProcessed)
	if gameProcessed then
		return 
	end
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Disable()
	end
end

local SprintingFunction = Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and SprintAnimation.IsPlaying == true and Humanoid.MoveDirection.Magnitude < 0.3 then
		Disable()
	end
end)


local function CharacterWasAdded(newCharacter)
	Character = newCharacter
	Humanoid = newCharacter:WaitForChild("Humanoid")
	
	Animation = Instance.new("Animation", Character)
	Animation.Name = "SprintAnimation"
	Animation.AnimationId = "rbxassetid://6285133880"
	SprintAnimation = Character.Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(Animation)
	SprintingFunction = Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
		if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and SprintAnimation.IsPlaying == true and Humanoid.MoveDirection.Magnitude < 0.3 then
			Disable()
		end
	end)
end

UserInputService.InputBegan:Connect(EnableSprinting)
UserInputService.InputEnded:Connect(DisableSprinting)
Player.CharacterAdded:Connect(CharacterWasAdded)
1 Like

I would really appreciate some advice if I need to change or clean anything.

you use Instance.new with a parent argument which is strongly advised against.

Change that part of the code to:

local defaultSpeed = 16
local sprintingSpeed = defaultSpeed * 1.5

Animation = Instance.new("Animation")
Animation.Name = "SprintAnimation"
Animation.AnimationId = "rbxassetid://6285133880"
Animation.Parent  = Character

local SprintAnimation  = Character.Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(Animation)

In your CharacterWasAdded local function, you make the same mistake above:

Change that part of the code to:

Animation = Instance.new("Animation")
Animation.Name = "SprintAnimation"
Animation.AnimationId = "rbxassetid://6285133880"
Animation.Parent  = Character

There is no point in reassigning the newCharacter parameter to character since they work the exact same way.

You could just do:

local function CharacterWasAdded(Character)
	Humanoid = Character:WaitForChild("Humanoid")
	
	Animation = Instance.new("Animation")
	Animation.Name = "SprintAnimation"
	Animation.AnimationId = "rbxassetid://6285133880"
    Animation.Parent = Character
	SprintAnimation = Character.Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(Animation)
	SprintingFunction = Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
		if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and SprintAnimation.IsPlaying == true and Humanoid.MoveDirection.Magnitude < 0.3 then
			Disable()
		end
	end)
end

Final optimized code:

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui

local MainGui = PlayerGui:WaitForChild("MainGui")
local SprintingVignette = MainGui:FindFirstChild("SprintingVignette")

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Camera = workspace.Camera

local defaultSpeed = 16
local sprintingSpeed = defaultSpeed * 1.5

local Animation = Instance.new("Animation")
Animation.Name = "SprintAnimation"
Animation.AnimationId = "rbxassetid://6285133880"
Animation.Parent = Character

local SprintAnimation = Character.Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(Animation)

local function Disable()
	TweenService:Create(Camera, TweenInfo.new(1), {FieldOfView = 70}):Play()
	TweenService:Create(SprintingVignette, TweenInfo.new(1), {ImageTransparency = 1}):Play()
	SprintAnimation:Stop()
	Humanoid.WalkSpeed = defaultSpeed
end

local function EnableSprinting(key, gameProcessed)
	if gameProcessed then
		Disable()
		return 
	end
	if key.KeyCode == Enum.KeyCode.LeftShift and (Humanoid.MoveDirection.Magnitude > 0) then
		TweenService:Create(Camera, TweenInfo.new(1), {FieldOfView = 80}):Play()
		TweenService:Create(SprintingVignette, TweenInfo.new(1), {ImageTransparency = 0.5}):Play()
		SprintAnimation:Play()
		Humanoid.WalkSpeed = sprintingSpeed
		SprintAnimation:AdjustSpeed(1.35)
	end
end

local function DisableSprinting(key, gameProcessed)
	if gameProcessed then
		return 
	end
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Disable()
	end
end

local SprintingFunction = Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and SprintAnimation.IsPlaying == true and Humanoid.MoveDirection.Magnitude < 0.3 then
		Disable()
	end
end)


local function CharacterWasAdded(character)
	Humanoid = Character:WaitForChild("Humanoid")

	Animation = Instance.new("Animation")
	Animation.Name = "SprintAnimation"
	Animation.AnimationId = "rbxassetid://6285133880"
	Animation.Parent = Character
	SprintAnimation = Character.Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(Animation)
	SprintingFunction = Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
		if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and SprintAnimation.IsPlaying == true and Humanoid.MoveDirection.Magnitude < 0.3 then
			Disable()
		end
	end)
end

UserInputService.InputBegan:Connect(EnableSprinting)
UserInputService.InputEnded:Connect(DisableSprinting)
Player.CharacterAdded:Connect(CharacterWasAdded)
1 Like

I suggest you read the rules because there’s nothing wrong with bumping one of your own posts that is only a day old that didn’t get any attention.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.