Game script only working in studio (UNSOLVED)

I am scripting a pirate-styled fighting game but I have a game script which controls the game it works perfectly in Roblox studio with no issues but when I test it on roblox.com when the round starts after a few seconds it kills all players in-game and says the game has ended when it shouldn’t have I have spent too long trying to figure this out with no success. I have a script which is supposed to detect when everybody on a team dies so it can trigger the game to end which lets the game script end the game which for some reason only works in Roblox Studio and not when I launch the game on Roblox.com here are video showing the issue

In Roblox Studio:

On Roblox.com:
Roblox Studio Bug2.mp4 - Google Drive

Main Game Script (Server)

local Rep = game:GetService("ReplicatedStorage")
local ValuesFolder = Rep:WaitForChild("Values")
local Events = Rep:WaitForChild("Events")
local TimeEvent = Events:WaitForChild("RemoteEvents"):WaitForChild("TimeEvent")
local HasGameEnded = ValuesFolder:WaitForChild("GameValues"):WaitForChild("GameStarted")

local shipsFolder = Rep:WaitForChild("Ships")
local barnacleShip = shipsFolder:WaitForChild("BarnacalsShip")
local SeaDogShip = shipsFolder:WaitForChild("SeaDogsShip")

local Teams = game:GetService("Teams")
local Barnacles = Teams:WaitForChild("Team Barnacles")
local SeaDogs = Teams:WaitForChild("Team Seadog")

local BarnaclesSpawn = game:GetService("Workspace"):WaitForChild("Spawns"):WaitForChild("BarnaclesSpawn")
local SeadogsSpawn = game.Workspace:WaitForChild("Spawns"):WaitForChild("SeaDogsSpawn")

local IngameShips = game.Workspace:WaitForChild("InGameShips")

local gameEndedEvent = Rep:WaitForChild('Events'):WaitForChild('BindableEvents'):WaitForChild('GameEndedEvent')
local WinningTeamEvent = Rep:WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('WinningTeamEvent')

local endGameChecker = Rep:WaitForChild('Events'):WaitForChild('BindableEvents'):WaitForChild('EndGameChecker')

local WinningTeam = nil

local function reset()
	print("Reset Func Called")
	if not WinningTeam then
		print(WinningTeam)
		WinningTeam = nil
	end
	HasGameEnded.Value = false
	WinningTeam = nil
	for _, player in pairs(game.Players:GetPlayers()) do
		player.Team = nil
	end
end

local function endGameEvent()
	if WinningTeam then
		print("All Players Dead!")
		reset()
	else
		print("Players Still Alive!")
		print(WinningTeam)
		local timeAmount = 30
		local timerText = "End Game Event!"
		local clone

		WinningTeamEvent:FireAllClients("Xf!")

		local randomEvents = game:GetService('ReplicatedStorage'):WaitForChild('RandomEvents')
		local events = randomEvents:GetChildren()

		HasGameEnded.Changed:Connect(function()
			if HasGameEnded.Value == false then
				game:GetService('Debris'):AddItem(clone,0)
				timeAmount = 0
				print("Reset Fired?")
				reset()
			end
		end)

		if #events > 0 then
			local randomIndex = math.random(1, 1) -- update when there are more events
			if randomIndex == 1 then
				for _,i in pairs(randomEvents:WaitForChild('UFO'):GetChildren()) do
					clone = i:Clone()
					clone.Name = "UFO"
					clone.Parent = game:GetService('Workspace'):WaitForChild('InGameEvents')
				end
			end

			while timeAmount > 0 do
				TimeEvent:FireAllClients(timeAmount,timerText)
				task.wait(1)
				timeAmount = timeAmount - 1
				if timeAmount == 0 and WinningTeam == nil then
					WinningTeam = nil
					WinningTeamEvent:FireAllClients("Gm!")
				end
				if timeAmount == 0 then
					for _,i in pairs(game:WaitForChild("Workspace"):WaitForChild('InGameEvents'):GetChildren()) do
						game:GetService('Debris'):AddItem(i,0)
						endGameChecker:Fire(WinningTeam)
					end
				end
			end
		end
	end
end

local function updWinningTeam()
	WinningTeam = nil
end

local function shuffle(tbl)
	local size = #tbl
	for i = size, 1, -1 do
		local rand = math.random(size)
		tbl[i], tbl[rand] = tbl[rand], tbl[i]
	end
	return tbl
end

local function assigningTeams()
	local timeAmount = 5
	local timerText = "Assigning Teams"

	local candidates = game:GetService("Players"):GetPlayers()
	local numCandidates = #candidates
	local playersPerTeam = math.ceil(numCandidates / 2)

	local shuffledCandidates = {}
	for _, player in ipairs(candidates) do
		table.insert(shuffledCandidates, player)
	end
	shuffle(shuffledCandidates)

	for i, player in ipairs(shuffledCandidates) do
		if i <= playersPerTeam then
			player.Team = Barnacles
		else
			player.Team = SeaDogs
		end
	end

	TimeEvent:FireAllClients(timeAmount, timerText)
end

local function teleportPlayer(plr, spawn)
	local character = plr.Character or plr.CharacterAdded:Wait()
	character:MoveTo(spawn.Position)
end

local function teleportingPlrs()
	local timeAmount = 5
	local timerText = "Teleporting Players"
	HasGameEnded.Value = true

	for _, player in pairs(game.Players:GetPlayers()) do
		if player.Team == Barnacles then
			teleportPlayer(player, BarnaclesSpawn)
		elseif player.Team == SeaDogs then
			teleportPlayer(player, SeadogsSpawn)
		end
	end

	TimeEvent:FireAllClients(timeAmount, timerText)
end

local function CleaningMap()
	local timeAmount = 5
	local TimerText = "Cleaning Up Map"

	for _,i in pairs(IngameShips:GetChildren()) do
		i:Destroy()
	end

	local InGameEvents = game:WaitForChild("Workspace"):WaitForChild('InGameEvents')

	for _,k in pairs(InGameEvents:GetChildren()) do
		game:GetService('Debris'):AddItem(k,0)
	end

	local clone1 = barnacleShip:Clone()
	clone1.Name = "Barnacle Ship"
	clone1.Parent = IngameShips

	local clone2 = SeaDogShip:Clone()
	clone2.Name = "SeaDog Ship"
	clone2.Parent = IngameShips

	TimeEvent:FireAllClients(timeAmount, TimerText)
end

local function playGame()
	print("Game Started")
	local timeAmount = 25
	local TimerText = "Round Active"

	while timeAmount > 0 do
		TimeEvent:FireAllClients(timeAmount, TimerText)
		task.wait(1)
		timeAmount = timeAmount - 1
		print(WinningTeam)

		gameEndedEvent.Event:Connect(function(Team)
			timeAmount = 0
			WinningTeam = Team
			WinningTeamEvent:FireAllClients(WinningTeam)
			endGameChecker:Fire(WinningTeam)
		end)
	end
end

local function Intermission()
	local intermission = 25
	local TimerText = "Intermission"
	while intermission > 0 do
		TimeEvent:FireAllClients(intermission, TimerText)
		task.wait(1)
		intermission = intermission - 1
	end
end

while true do
	reset()
	CleaningMap()
	Intermission()
	assigningTeams()
	teleportingPlrs()
	playGame()
	endGameEvent()
end

Check If Game Over Script (Server)

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local CreatePlayerDeathEvent = ReplicatedStorage:WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('CreatePlayerDeathEvent')
local HasGameStartedVal = ReplicatedStorage:WaitForChild('Values'):WaitForChild('GameValues'):WaitForChild('GameStarted')
local PlayerDeathCheckerFolder = ReplicatedStorage:WaitForChild('Values'):WaitForChild('PlayerDeathCheckerFolder')
local RemovePlayerDeaths = ReplicatedStorage:WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('RemovePlayerDeathsEvent')
local ChangePlayerDeathState = ReplicatedStorage:WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('ChangePlayerDeathState')
local OpenSpectateMenuEvent = ReplicatedStorage:WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('OpenSpectateMenu')
local PlayersAliveInt = PlayerDeathCheckerFolder:WaitForChild('PlayersAlive')
local PlayerDiedEvent = ReplicatedStorage:WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('PlayerDied')
local gameJustEndedEvent = ReplicatedStorage:WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('GameJustEndedEvent')
local Rep = game:GetService('ReplicatedStorage')

local TeamsService = game:GetService('Teams')
local SeaDogsTeam = TeamsService:WaitForChild('Team Seadog')
local BarnaclesTeam = TeamsService:WaitForChild('Team Barnacles')

local SeaDogsAlive = Rep:WaitForChild('Values'):WaitForChild('PlayerDeathCheckerFolder'):WaitForChild('SeaDogsAlive')
local BarnaclesAlive = Rep:WaitForChild('Values'):WaitForChild("PlayerDeathCheckerFolder"):WaitForChild('BarnaclesAlive')

local gameEndedEvent = ReplicatedStorage:WaitForChild('Events'):WaitForChild('BindableEvents'):WaitForChild('GameEndedEvent')

local CooldownTime = 5
local PlayerCooldowns = {}

local function CheckIfGameOver()
	for _, player in pairs(game.Players:GetPlayers()) do
		if not PlayerCooldowns[player] or os.time() - PlayerCooldowns[player] >= CooldownTime then
			PlayerCooldowns[player] = os.time()

			local team = player.Team
			if team then
				local allDead = true
				for _, teamMember in pairs(team:GetPlayers()) do
					local value = PlayerDeathCheckerFolder:FindFirstChild(teamMember.Name.." Is Dead?")
					if value then
						if not value.Value then
							allDead = false
							break
						end
					end
				end
				if allDead then
					if team == SeaDogsTeam then
						SeaDogsAlive.Value = SeaDogsAlive.Value - 1
						if SeaDogsAlive.Value <= 0 then
							HasGameStartedVal.Value = false
							gameEndedEvent:Fire("Barnacles")
							print("Game Over!")
						end
					elseif team == BarnaclesTeam then
						BarnaclesAlive.Value = BarnaclesAlive.Value - 1
						if BarnaclesAlive.Value <= 0 then
							HasGameStartedVal.Value = false
							gameEndedEvent:Fire("SeaDogs")
							print("Game Over!")
						end
					end
				end
			end
		end
	end
end

local function updatePlayerCount()
	if HasGameStartedVal.Value == true then
		PlayersAliveInt.Value = #game.Players:GetPlayers()
	end
end

PlayerDiedEvent.OnServerEvent:Connect(updatePlayerCount)

HasGameStartedVal.Changed:Connect(function()
	if HasGameStartedVal.Value == false then
		for _, boolValue in pairs(PlayerDeathCheckerFolder:GetChildren()) do
			if boolValue:IsA("BoolValue") then
				boolValue.Value = false
			end
		end
		PlayersAliveInt.Value = #game:GetService('Players'):GetPlayers()
		for _, player in pairs(game.Players:GetPlayers()) do
			if player.Character and player.Character:FindFirstChildOfClass("Humanoid") then
				player.Character:FindFirstChildOfClass("Humanoid"):TakeDamage(math.huge)
				BarnaclesAlive.Value = 0
				SeaDogsAlive.Value = 0
			end
		end
	end
end)

ChangePlayerDeathState.OnServerEvent:Connect(function(plr, plrName, val)
	print("Received player name:", plrName)
	if HasGameStartedVal.Value == true then
		local Value = PlayerDeathCheckerFolder:FindFirstChild(plrName.." Is Dead?")
		if plrName then
			Value.Value = tostring(val)
			OpenSpectateMenuEvent:FireClient(plr, true)
		else
			print("value not found")
		end
	else
		OpenSpectateMenuEvent:FireClient(plr, false)
	end
end)

RemovePlayerDeaths.OnServerEvent:Connect(function(plr,plrName)
	local val = PlayerDeathCheckerFolder:FindFirstChild(plrName.." Is Dead?")
	if val then
		val:Destroy()
	end
end)

CreatePlayerDeathEvent.OnServerEvent:Connect(function(plr,plrName)
	if not PlayerDeathCheckerFolder:FindFirstChild(plrName.." Is Dead?") then
		local val = Instance.new("BoolValue")
		val.Name = plrName.." Is Dead?"
		val.Parent = PlayerDeathCheckerFolder
	end
end)

local function updatePlayerCount2()
	PlayersAliveInt.Value = #game.Players:GetPlayers()
end

task.wait(5)
updatePlayerCount2()

HasGameStartedVal.Changed:Connect(updatePlayerCount2)

gameJustEndedEvent.OnServerEvent:Connect(updatePlayerCount2)

while true do
	CheckIfGameOver()
	task.wait()
end