Attempt to index nil with 'Play'

-- SERVICES --
-----------------------------
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
----------------------------
-- VARIABLES --
-----------------------------
local player = Players.LocalPlayer
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid") :: Humanoid
local camera = workspace.CurrentCamera

local gui = player.PlayerGui:WaitForChild("StaminaGui")

local currentStamina = 100
local shiftDown = false
local sprintingCooldown = false
local canReplenishStamina = true
-----------------------------
-- CONSTANTS --
-----------------------------
local SPRINT_SPEED = 22
local REGULAR_SPEED = humanoid.WalkSpeed
local MAX_STAMINA = 100
local STAMINA_REDUCE = 32
local STAMINA_INCREASE = 16
local SPRINT_FOV = 85
local REGULAR_FOV = camera.FieldOfView
local SPRINT_COOLDOWN = 5
-----------------------------
-- PRIVATE FUNCTIONS --
-----------------------------
local function startSprint()
	if camera.FieldOfView ~= SPRINT_FOV then
		TweenService:Create(camera, TweenInfo.new(0.8), {FieldOfView = SPRINT_FOV}):Play()
	end
	humanoid.WalkSpeed = SPRINT_SPEED
end

local function resetSpeed()
	if camera.FieldOfView ~= REGULAR_FOV then
		TweenService:Create(camera, TweenInfo.new(0.8), {FieldOfView = REGULAR_FOV}):Play()
	end
	humanoid.WalkSpeed = REGULAR_SPEED
end


local function updateStaminaBar()
	if not gui.Enabled then
		gui.Enabled = true
		TweenService:Create(gui.Border.Outline, TweenInfo.new(0.3), {Transparency = 0}):Play()
		TweenService:Create(gui.Border.Fill, TweenInfo.new(0.3), {BackgroundTransparency = 0}):Play()
	end
	
	gui.Border.Fill.Size = UDim2.fromScale(1, currentStamina/MAX_STAMINA)
	
	if (currentStamina >= 100 or gui.Border.Fill.Size.Y.Scale == 1) and gui.Border.Outline.Transparency == 0 then
		TweenService:Create(gui.Border.Outline, TweenInfo.new(0.3), {Transparency = 1}):Play()
		local tween = TweenService:Create(gui.Border.Fill, TweenInfo.new(0.3), {BackgroundTransparency = 1}):Play()
		tween:Play()
		tween.Completed:Connect(function()
			gui.Enabled = false
		end)
	end
end
-----------------------------
-- HANDLERS --
-----------------------------
ContextActionService:BindAction("Sprint", function(actionName, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin then
		if currentStamina <= 0 then
			return
		end
		shiftDown = true
		startSprint()
	elseif inputState == Enum.UserInputState.End then
		shiftDown = false
		resetSpeed()
	end
end, true, Enum.KeyCode.LeftShift)

humanoid.Died:Connect(function()
	ContextActionService:UnbindAction("Sprint")
end)

RunService.Heartbeat:Connect(function(deltaTime)
	if shiftDown and currentStamina > 0 and humanoid.MoveDirection.Magnitude > 0 then
		currentStamina -= STAMINA_REDUCE * deltaTime
	elseif not sprintingCooldown then
		if currentStamina <= 0 then
			sprintingCooldown = true
			canReplenishStamina = false
			shiftDown = false
			currentStamina = 0
			resetSpeed()
			task.delay(SPRINT_COOLDOWN, function()
				canReplenishStamina = true
			end)
		end
	end
	
	if currentStamina < MAX_STAMINA then
		if not canReplenishStamina then
			return
		end
		if not shiftDown or humanoid.MoveDirection.Magnitude <= 0 then
			currentStamina += STAMINA_INCREASE * deltaTime
			sprintingCooldown = false
		end
		 updateStaminaBar()
	end
end)```

I’ve created this code and I am really unsure about why am I getting this error. I’ve looked through the code and there seems to be nothing wrong. Any help?

Did you tried going to the console and see if it says where the error is?

Where the error happens?

IGNORE THIS

It happens when I attempt to play the tween.

		tween:Play()
		tween.Completed:Connect(function()
			gui.Enabled = false

Here ^^^

That means the “tween” is nil.
Maybe when generating Tween, gui.Border.Fill may got interrupted/destroyed.
check for a line where it deletes gui.Border.Fill

local tween = TweenService:Create(gui.Border.Fill, TweenInfo.new(0.3), {BackgroundTransparency = 1})
tween:Play()

this should fix that

this was causing the script to error

Silly me :sweat_smile:, I have no idea how I glossed over that.

Thanks everyone for the help!