Playing a sound locally to the player after he leaves the game

  1. What do you want to achieve? Play a sound locally if player leaves the game.

  2. What is the issue? Roblox closes too quickly.

  3. What solutions have you tried so far? I have tried different approaches for the code such as ancestrychanged or even bindtoclose function which i found out can only be used on server unfortunately.

Hi, I would want to code a feature where if the player leaves the game it would play a sort of goodbye sound to him. The problem is that if player leaves the game, the roblox process ends too quickly for it to play the sound fully (around 100 milliseconds i believe). I noticed that one game has achieved it and I just can’t seem to figure out how. If you would like to visit it and check for yourself here’s the link: Habitat 84 - Roblox
After joining just leave the game and listen. While doing this i noticed in the task manager that roblox process takes longer to actually end so it might have to do something with that i presume.

local plr = game.Players.LocalPlayer

game.Players.PlayerRemoving:Connect(function(rmvplr)
	
	if rmvplr == plr then
		print("te")
		local goodbyeSound = script:FindFirstChild("heiswatching"):Clone()
		goodbyeSound.Parent = workspace
		goodbyeSound:Play()
		print("E")
		wait(goodbyeSound.TimeLength)
		print("st")
	end
end)

In the code only “te” and “E” gets printed out.

Would be grateful for any help!

1 Like

Hey, this is an interesting idea, idea is, try this event, it might work and probably won’t. Can’t test it right now as I have to log off soon, yet, there’s another way it always worked on me. Sound with the PlayOnRemove prop on gets played when disconnected. You’ll have to find though which objects get deleted on disconnection for it to work, which can be tricky. Goodluck, and, wish you a good night/day.

2 Likes

Instead of using Players.PlayerRemoving, use game.OnClose instead, and make sure it’s a LocalScript.

local Players = game:GetService("Players")
local player = Players.LocalPlayer

game.OnClose = function()
	local goodbyeSound = script:FindFirstChild("heiswatching"):Clone()
	goodbyeSound.Parent = player.PlayerGui -- // Added this in PlayerGui instead of workspace
	goodbyeSound:Play()
	task.wait(goodbyeSound.TimeLength)
end
2 Likes

Thank you for y’alls replies! The game.OnClose function eventually fixed the issue. Thank you both for willing to help :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.