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


--[==[
    Разместите этот LocalScript внутри ScreenGui "Police"
    Внутри Police должны быть:
    - StatusBox (TextLabel) для фразы (он будет качаться)
    - TimeBox (TextLabel) для цифр (остаётся ровным)
]==]

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local gui = script.Parent
local staticLabel = gui:WaitForChild("StatusBox")   -- фраза (будет качаться)
local timerLabel = gui:WaitForChild("TimeBox")      -- цифры (неподвижны)

-- Настройка вращения вокруг центра для фразы
staticLabel.AnchorPoint = Vector2.new(0.5, 0.5)

local timerEvent = ReplicatedStorage:WaitForChild("StartTimerEvent")

-- Создаём RemoteEvent для сообщения о смерти
local timerExpiredEvent = ReplicatedStorage:FindFirstChild("TimerExpiredEvent")
if not timerExpiredEvent then
	timerExpiredEvent = Instance.new("RemoteEvent")
	timerExpiredEvent.Name = "TimerExpiredEvent"
	timerExpiredEvent.Parent = ReplicatedStorage
end

local currentPrintCoroutine = nil
local currentCountdown = nil
local pendulumConnection = nil

-- Форматирование времени с квадратными скобками
local function formatTime(seconds)
	local minutes = math.floor(seconds / 60)
	local secs = seconds % 60
	local timeString = string.format("%02d:%02d", minutes, secs)
	return "[" .. timeString .. "]"
end

-- Остановка маятника (для фразы)
local function stopPendulum()
	if pendulumConnection then
		pendulumConnection:Disconnect()
		pendulumConnection = nil
	end
	staticLabel.Rotation = 0
end

-- Запуск маятника для фразы
local function startPendulum()
	if pendulumConnection then
		pendulumConnection:Disconnect()
	end
	local amplitude = 12
	local speed = 1
	local time = 0
	pendulumConnection = RunService.RenderStepped:Connect(function(dt)
		time = time + dt * speed
		staticLabel.Rotation = amplitude * math.sin(time)
	end)
end

-- Полная остановка всего
local function stopEverything()
	if currentPrintCoroutine then
		coroutine.close(currentPrintCoroutine)
		currentPrintCoroutine = nil
	end
	if currentCountdown then
		coroutine.close(currentCountdown)
		currentCountdown = nil
	end
	stopPendulum()
	gui.Enabled = false
	staticLabel.Text = ""
	timerLabel.Text = ""
end

-- Анимация печати статической фразы
local function animateTyping(fullText, durationPerChar)
	staticLabel.Text = ""
	local typed = ""
	for i = 1, #fullText do
		typed = typed .. fullText:sub(i, i)
		staticLabel.Text = typed
		task.wait(durationPerChar)
		if not currentPrintCoroutine then return false end
	end
	return true
end

-- Запуск таймера (вызывается с сервера)
local function startCountdown(_)  -- игнорируем переданное сервером время
	stopEverything()
	gui.Enabled = true

	local fixedDuration = 300  -- 3 минуты (3 * 60 секунд)
	local staticPhrase = "TIME UNTIL LAW ENFORCEMENT ARRIVES ON THE SCENE:"

	currentPrintCoroutine = coroutine.create(function()
		local success = animateTyping(staticPhrase, 0.07)
		if not success then return end

		timerLabel.Text = formatTime(fixedDuration)
		currentPrintCoroutine = nil

		startPendulum()  -- теперь качается фраза

		currentCountdown = coroutine.create(function()
			for i = fixedDuration, 0, -1 do
				timerLabel.Text = formatTime(i)
				task.wait(1)
			end
			timerExpiredEvent:FireServer()
			stopEverything()
		end)
		coroutine.resume(currentCountdown)
	end)
	coroutine.resume(currentPrintCoroutine)
end

timerEvent.OnClientEvent:Connect(startCountdown)