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)