Playing sounds for a local player after they leave the game

I’ve been trying to make a script that plays a sound after a player has left the game in a way that they can still hear the sound themselves. As far as I’m concerned, the issue I’ve run into is that Roblox deletes the script way too fast for it to work. However, I’ve seen many horror games use such an effect. I’m wondering if there’s a known bypass around this. Any help or input is appreciated.

The code I’m currently using:

local Players = game:GetService("Players")
local sound = game.workspace.TestSound
local function playLeaveSound(player)
    sound.Parent = SoundService
    sound:Play()
end

Players.PlayerRemoving:Connect(playLeaveSound)
2 Likes

Try doing the .PlayerRemoving on the server side and using an event to activate the localscript. It worked for me when I was trying something like this.

4 Likes

I’ve tried this but still no luck.

Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local playLeaveSoundEvent = game.ReplicatedStorage.PlayLeaveSoundEvent

local function playLeaveSound(player)
	playLeaveSoundEvent:FireClient(player)
	wait(1) 
end

Players.PlayerRemoving:Connect(playLeaveSound)

LocalScript

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local playLeaveSoundEvent = ReplicatedStorage:WaitForChild("PlayLeaveSoundEvent")

playLeaveSoundEvent.OnClientEvent:Connect(function()
    local sound = game.SoundService:FindFirstChild("TestSound")
	sound.Parent = player:FindFirstChild("PlayerGui") or player:FindFirstChild("Backpack") or workspace
	sound:Play()
	sound.Ended:Wait()
	sound:Destroy()
end)

anything specifically wrong with these?

2 Likes

where are the ServerScript,LocalScript placed?

3 Likes