Making a sound play when a player leaves

Hello!

I’ve been playing a game called pressure, and when I leave or even if I do ALT+F4 a person says bye to me. I wanna make a script to do this but have been unable to do it.

Here’s the progress some others have made so far but it does not work.

-- // Server

local PlayerService = game:GetService("Players")
local ByeSound: Sound = game:GetService("SoundService"):FindFirstChild("Bye")

PlayerService.PlayerRemoving:Connect(function(player: Player)
	local SoundClone = ByeSound:Clone()

	SoundClone.Parent = player
	SoundClone:Play()
end)

Parenting to the player will not work, because that player is being removed, therefore that sound will be removed. What you want to do is spawn a part where the player’s character last was, then parent the sound to that. Then eventually destroy that part when the sound is done playing.

1 Like

Roblox deletes/removes everything in the workspace when leaving a game; Sounds have a property named “PlayOnRemove” that can be enabled. I’m sure you can figure the rest out from there.

try this, parent it to the soundservice instead. also make sure to delete the sound once you don’t need it

-- // Server

local PlayerService = game:GetService("Players")
local SoundService = game:GetService("SoundService")

local ByeSound: Sound = SoundService:FindFirstChild("Bye")

PlayerService.PlayerRemoving:Connect(function(player: Player)
	local SoundClone = ByeSound:Clone()

	SoundClone.Parent = SoundService 
	SoundClone:Play()
end)

Just a tip, try that Search button first. I searched “play a sound when player leaves” and it shows a few posts about this exact same question.

local CanCloseServer = false
local RunService = game:GetService('RunService')
local ByeSound: Sound = game:GetService("SoundService"):FindFirstChild("Bye")

game:BindToClose(function()
	while RunService:IsRunning() and not CanCloseServer do
		task.wait()
	end
end)
game.Players.PlayerRemoving:Connect(function(Player)
      local SoundClone = ByeSound:Clone()
      SoundClone.Parent = player
      SoundClone.PlayOnRemove = true
      SoundClone:Destroy()
      CanCloseServer = true
end)
2 Likes
-- Main Dependencies
-- rlly rlly cool!!!
local Players : Players = game:GetService"Players";
local RunService : RunService = game:GetService"RunService";
local LocalPlayer : Player? = Players.LocalPlayer;

-- Place an sound object inside of the script 
-- (NOTICE: the name doesn't matter since we find the first child withthe class 'Sound')
local SoundObject : Sound = script:FindFirstChildOfClass"Sound";

-- Making sure the sound is loaded.
if not SoundObject.IsLoaded then 
	SoundObject.Loaded:Wait(); 
end;
local ExtendedTimeLength : number = SoundObject.TimeLength + 0.1;

local function LeaveAudio(player:Player?) : ()
	if not RunService:IsStudio() and player == LocalPlayer then
		-- Stops all sounds from overlaping the leaving sound.
		for i, soundObject in pairs(workspace:GetDescendants()) do
			if soundObject:IsA("Sound") then
				soundObject:Stop();
			end;
		end;
			
		-- Play the damn thing.
		SoundObject:Play();
		
		-- forces the game to lag allowing the audio to play.
		local tickResult : number = tick();
		while true do
			local i:number, v:number = 1, nil;
			
			for o = i, math.huge do
				v = tick() - tickResult;
				if ExtendedTimeLength < v then return; end;
			end;

			if ExtendedTimeLength < tick() - tickResult then return; end;
		end;
	end;
end

Players.PlayerRemoving:Connect(LeaveAudio);