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


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")
local sound = gui:WaitForChild("calv")
staticLabel.AnchorPoint = Vector2.new(0.5, 0.5)
local timerEvent = ReplicatedStorage:WaitForChild("StartTimerEvent")
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 = 2.5
	local time = 0
	pendulumConnection = RunService.RenderStepped:Connect(function(dt)
		time += dt * speed
		staticLabel.Rotation = amplitude * math.sin(time)
	end)
end
local function stopEverything()
	if currentPrintCoroutine then
		pcall(coroutine.close, currentPrintCoroutine)
		currentPrintCoroutine = nil
	end
	if currentCountdown then
		pcall(coroutine.close, currentCountdown)
		currentCountdown = nil
	end
	stopPendulum()
	if sound then
		sound:Stop()
	end
	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
	-- Запускаем звук один раз
	sound:Stop()
	sound.Looped = false
	sound.TimePosition = 0
	sound:Play()
	local fixedDuration = 120
	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()
			stopPendulum()
			staticLabel.Text = ""
			timerLabel.Text = ""
			startPendulum()
			local finalMessage = "FUCK! POLICE ARRIVED"
			for i = 1, #finalMessage do
				staticLabel.Text = finalMessage:sub(1, i)
				task.wait(0.08)
			end
			task.wait(20)
			stopEverything()
		end)
		coroutine.resume(currentCountdown)
	end)
	coroutine.resume(currentPrintCoroutine)
end
timerEvent.OnClientEvent:Connect(startCountdown)