--Receive announcements

local goalTransparency = announcement.Transparency

function createAnnouncement(message)
	announcement.AnnouncementText.Text = ""
	announcement.AnnouncementText.TextTransparency = 0
	announcement.Transparency = 1
	announcement.Visible = true

	local fadeTweenInfo = TweenInfo.new(0.4, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
	local fadeInTween = ts:Create(announcement, fadeTweenInfo, {Transparency = goalTransparency})
	fadeInTween:Play()
	task.wait(0.3)

	for i = 1, string.len(message) do
		announcement.AnnouncementText.Text = string.sub(message, 1, i)
		local currentChar = string.sub(message, i, i)
		task.wait(currentChar == "\n" and 0.3 or currentChar == " " and 0.04 or 0.02)
	end

	local announcementTime = string.len(message) / 16
	task.wait(announcementTime)

	local fadeOutTween = ts:Create(announcement, fadeTweenInfo, {Transparency = 1})
	fadeOutTween:Play()
	local fadeTextTween = ts:Create(announcement.AnnouncementText, fadeTweenInfo, {TextTransparency = 1})
	fadeTextTween:Play()
	
	fadeTextTween.Completed:Wait()
	announcement.Visible = false
end

re.OnClientEvent:Connect(function(instruction, message)
	if instruction == "ANNOUNCE" then
		createAnnouncement(message)
	end
end)

