Sprint Script stops working after respawn

I have a sprinting system in my game, but when were you respawn the system stops working.

Sprinting is toggled by pressing Right Shift.

I have tried many things but I can’t figure it out. If you know to fix this then, please tell me.

SCP Foundation ALPHA - Roblox Studio 05.02.2022 12_11_06_LI

local bar = script.Parent

local stamina = 100
local staminaRate = 1

local plr = game.Players.LocalPlayer
local char = plr.Character or plr:GetPropertyChangedSignal("Character"):Wait()
char = plr.Character

local humanoid = char:WaitForChild("Humanoid")


local isSprinting = false


local uis = game:GetService("UserInputService")


uis.InputBegan:Connect(function(key, gameProcessed)
	
	if gameProcessed or stamina == 0 then return end
	
	
	if key.KeyCode == Enum.KeyCode.LeftShift then
		
		isSprinting = not isSprinting
		
		if isSprinting then
			humanoid.WalkSpeed = 25
		else
			humanoid.WalkSpeed = 16
		end
	end
end)


while wait() do
	
		
	if stamina == 0 and isSprinting then
		
		isSprinting = false
		humanoid.WalkSpeed = 16
	end
	
	
	if isSprinting and humanoid.MoveDirection.Magnitude > 0 then
		
		stamina = stamina - 1
		wait(staminaRate)
		
	else
		stamina = stamina + 1
		wait(staminaRate)
	end
	
	
	stamina = math.clamp(stamina, 0, 100)
	
	bar:TweenSize(UDim2.new((1/100) * stamina, 0, 1 ,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.5)
end

When turning ResetOnSpawn on the Script works but the UI element disappears

Before Respawn

After Respawn

Does your ScreenGui have the property ResetOnSpawn set to true? If not then try setting it to true and see if that works.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr:GetPropertyChangedSignal("Character"):Wait()
char = plr.Character

Use this block of code to define the player/character.

it doesn’t fix it. Currently (with ResetOnSpawn on) the player respawns the sprinting works but the Sprint Bar UI is missing. As seen by the images above