Assistance with stamina script

Hi, Ive implemented a script into my game kindly provided by an okeanskiy tutorial, however there is a confliction with my game in regards to the game having a lobby, then teleporting players into the map, as the stamina script works in the lobby but when players are teleported into the selected map the script no longer functions.

This is the script:

local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")

repeat wait() until players.LocalPlayer.Character
local character = players.LocalPlayer.Character

local maxStamina = 250
local staminaRegen = 1
local staminaCost = 5

local sprintSpeed = 24
local walkSpeed = 16

local currentStamina = maxStamina

-- Update Stamina GUI
function updateGui(current, max)
	if character.Humanoid.Health <= 0 then
		players.LocalPlayer.PlayerGui.Stamina.Enabled = false
	end

	players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new((current / max)* 0.2, 0,0.05, 0)
end

-- Sprint Key Pressed
userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then
			character.Humanoid.WalkSpeed = sprintSpeed
		end
	end
end)

-- Sprint Key Released
userInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		character.Humanoid.WalkSpeed = walkSpeed
	end
end)



runService.Heartbeat:Connect(function()
	if character.Humanoid.WalkSpeed == sprintSpeed then
		if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then
			currentStamina = currentStamina - staminaCost
		else
			character.Humanoid.WalkSpeed = walkSpeed
		end
	else
		if currentStamina < maxStamina then
			currentStamina = currentStamina + staminaRegen
		elseif currentStamina > maxStamina then
			currentStamina = maxStamina
		end
	end

	updateGui(currentStamina, maxStamina)
end)

When the lobby counts down, the map is chosen, and the players teleport into the map. I attempted a line of code before the current ‘repeat’ line saying something like:

repeat wait() until workspace:FindFirstChild("Map")

(When the chosen map pops into the workspace for the players to tp to) However this didnt work. Anyone have any ideas on how this can conveniently be adjusted to activate the stamina script only when in the map (or both lobby and map for that matter?)
To note, I have value tags which players are assigned to when tped into the map, and also individual teams (which both can be called possibly if that helps, and also with the map getting added into the workspace when the players are about to tp in)

hope this makes sense
thanks

Try this instead

if currentStamina <= maxStamina then

Try

elseif currentStamina >= maxStamina then

This wont affect the actual running of the script will it just better coding? And thanks il add that in

Soo do you want it to work only in the map?

Yeah ideally, so when the players are teleported (and i believe respawned so this happens) the stamina function no longer works as expected

Just make bool value inside of player and you will make that value true when you teleport the player to map and false when you teleport the player back to lobby and in your script you will check if the value is true so the stamina will work if the value is false then nothing will happen.