Загрузка данных


 local Players = game:GetService("Players")

-- Настройки времени
local INTERMISSION_TIME = 20 -- Время на спавне (пока сделаем 20 сек, чтобы быстрее тестировать)
local RACE_TIME = 90 -- Время гонки (1.5 минуты)

local flyZone = workspace:FindFirstChild("FlyZone")
local isRaceActive = false
local flyingPlayers = {}

-- Глобальный таймер (ячейка памяти)
local gameTime = Instance.new("IntValue")
gameTime.Name = "GameTime"
gameTime.Value = INTERMISSION_TIME
gameTime.Parent = game.ReplicatedStorage

task.spawn(function()
	while true do
		-- === ЭТАП 1: СПАВН ===
		isRaceActive = false
		for i = INTERMISSION_TIME, 0, -1 do
			gameTime.Value = i
			task.wait(1)
		end
		
		-- === ЭТАП 2: ТЕЛЕПОРТ ===
		isRaceActive = true
		for _, player in pairs(Players:GetPlayers()) do
			local char = player.Character
			if char and char:FindFirstChild("HumanoidRootPart") and flyZone then
				char.HumanoidRootPart.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
				-- Телепортируем строго в центр FlyZone, чуть выше дна
				char.HumanoidRootPart.CFrame = CFrame.new(flyZone.Position) * CFrame.new(0, -flyZone.Size.Y/2 + 5, 0)
			end
		end
		
		-- Идет гонка
		for i = RACE_TIME, 0, -1 do
			gameTime.Value = i
			task.wait(1)
		end
		
		-- === ЭТАП 3: КОНЕЦ ГОНКИ ===
		isRaceActive = false
		flyingPlayers = {}
		for _, player in pairs(Players:GetPlayers()) do
			player:LoadCharacter() -- Возвращаем на спавн
		end
	end
end)

-- Логика полета
if flyZone then
	flyZone.Touched:Connect(function(hit)
		if not isRaceActive then return end
		local character = hit.Parent
		local player = Players:GetPlayerFromCharacter(character)
		
		if player and not flyingPlayers[player.UserId] then
			local leaderstats = player:WaitForChild("leaderstats")
			local speed = leaderstats:WaitForChild("Speed")
			local stamina = leaderstats:WaitForChild("Stamina")
			local coins = leaderstats:WaitForChild("Coins")
			
			if stamina.Value <= 0 then return end
			flyingPlayers[player.UserId] = true
			
			local hrp = character:WaitForChild("HumanoidRootPart")
			local startY = hrp.Position.Y
			
			local bodyVelocity = Instance.new("BodyVelocity")
			bodyVelocity.MaxForce = Vector3.new(0, 500000, 0) 
			-- Даем базовый толчок +30, чтобы игрок летел в любом случае, плюс его накликанная скорость!
			bodyVelocity.Velocity = Vector3.new(0, 30 + (speed.Value * 15), 0)
			bodyVelocity.Parent = hrp
			
			while isRaceActive and flyingPlayers[player.UserId] and stamina.Value > 0 do
				task.wait(1)
				stamina.Value = stamina.Value - 1
				bodyVelocity.Velocity = Vector3.new(0, 30 + (speed.Value * 15), 0)
			end
			
			bodyVelocity:Destroy()
			
			local endY = hrp.Position.Y
			local distance = math.max(0, endY - startY)
			local earnedCoins = math.floor(distance / 10)
			if earnedCoins > 0 then
				coins.Value = coins.Value + earnedCoins
			end
		end
	end)
end