Put a sound as soon as the GUI is displayed?

Hello everybody

So, I’m just a fan of a scripter but I’ll need a “mini” problem if I can put it that way

In fact, I created a softshutdown and the problem is that I would like there to be a sound when the shutdown is displayed.

It may seem easy for you but I’m having a hard time :sweat_smile:

I can send the code for a moment

local teleportService = game:GetService("TeleportService")
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local players = game:GetService("Players")

local messageGui = script:WaitForChild("Message")

local function newMessage(player, message, isStart)
	local playerMessage = messageGui:Clone()
	local messageLabel = playerMessage:WaitForChild("MessageLabel")
	
	messageLabel.Text = message
	
	playerMessage.Parent = player:FindFirstChild("PlayerGui")
	
	if isStart == true then
		local fadeTweenInfo = TweenInfo.new(
			0.5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		for _, v in pairs(playerMessage:GetChildren()) do
			if v:IsA("TextLabel") then
				tweenService:Create(v, fadeTweenInfo, {TextTransparency = 0}):Play()
			elseif v:IsA("ImageLabel") then
				tweenService:Create(v, fadeTweenInfo, {ImageTransparency = 0}):Play()
			end
		end
	else
		for _, v in pairs(playerMessage:GetChildren()) do
			if v:IsA("TextLabel") then
				v.TextTransparency = 0
			elseif v:IsA("ImageLabel") then
				v.ImageTransparency = 0
			end
		end
	end
end

if game.PrivateServerId ~= "" and game.PrivateServerOwnerId == 0 then
	local waitTime = 3
	
	players.PlayerAdded:Connect(function(player)
		newMessage(player, "The server is shutting down for maintenance/update. You will be redirected in a moment.", false)
		
		wait(waitTime)
		waitTime /= 2
		
		teleportService:Teleport(game.PlaceId, player)
	end)
	
	for _, v in pairs(players:GetPlayers()) do
		newMessage(v, "The server is shutting down for maintenance/update. You will be redirected in a moment.", false)
		
		teleportService:Teleport(game.PlaceId, v)
		
		wait(waitTime)
		waitTime /= 2
	end
elseif runService:IsStudio() == false then
	game:BindToClose(function()
		coroutine.resume(coroutine.create(function()
			local reservedServer = teleportService:ReserveServer(game.PlaceId)
			for _, v in pairs(players:GetPlayers()) do
				newMessage(v, "The server is shutting down for maintenance/update. You will be redirected in a moment.", true)
				
				teleportService:TeleportToPrivateServer(game.PlaceId, reservedServer, {v})
			end
			
			players.PlayerAdded:Connect(function(player)
				newMessage(player, "The server is shutting down for maintenance/update. You will be redirected in a moment.", true)
				
				teleportService:TeleportToPrivateServer(game.PlaceId, reservedServer, {player})
			end)
		end))
		
		while #players:GetPlayers() > 0 do
			wait(1)
		end
	end)
end

…can’t you just put the sound in a local file and play it locally before sending the message?

local function newMessage(player, message, isStart)
	local playerMessage = messageGui:Clone()
	local messageLabel = playerMessage:WaitForChild("MessageLabel")
	
	messageLabel.Text = message

    **play the sound here** EX: script.Parent.Sound:Play()
	
	playerMessage.Parent = player:FindFirstChild("PlayerGui")
	
	if isStart == true then
		local fadeTweenInfo = TweenInfo.new(
			0.5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		for _, v in pairs(playerMessage:GetChildren()) do
			if v:IsA("TextLabel") then
				tweenService:Create(v, fadeTweenInfo, {TextTransparency = 0}):Play()
			elseif v:IsA("ImageLabel") then
				tweenService:Create(v, fadeTweenInfo, {ImageTransparency = 0}):Play()
			end
		end
	else
		for _, v in pairs(playerMessage:GetChildren()) do
			if v:IsA("TextLabel") then
				v.TextTransparency = 0
			elseif v:IsA("ImageLabel") then
				v.ImageTransparency = 0
			end
		end
	end
end