What code to use for when the player leaves

Just to clarify from the title, I want to clone the player and put them inside of ServerStorage when they leave. The problem is that I don’t know what to write for when the player leaves.

1 Like

Is there a particular reason you want to store them into the ServerStorage? And is it that you want to store their character information?

You could look at Players.PlayerRemoving to get the character cloned, PlayerRemoving is called when a player leaves the server

1 Like

I found a script that basically loads a character inside of ServerStorage. I’m trying to make a character saving system. I want to clone the character when they leave, put it into ServerStorage, then load it back in when they rejoin

1 Like

That wouldnt work though, what if they join a different server?
You should use datastores to save data.

That won’t be an efficent way of saving the player, you want to use Data Stores for when players leave to save relevant information and load when they rejoin to get the information and set it onto the players.

game.Players.PlayerRemoving:Connect(function(player)
local character = player.Character
local charClone = character:Clone()
charClone.Parent = game.ServerStorage
end)

I don’t really understand the concept, but this is what I think you are talking about.

What exactly are you trying to save about the player when they leave?

If it is just the outfit and accessories of the player, then as Soccerver and SnaspantsMIxer have said, you should instead use Data Stores to ensure that this data is saved on all servers, as a player is almost never going to join the exact same server between any two given visits (And that server might not even exist anymore if all players leave!)

You should use something like this when the player is leaving (Psuedo-code so this will not work in game), which would get all of the ID’s for everything that the player is currently wearing (assuming they are wearing things from the Catalog and not custom in-game items) to be saved into a datastore

-- You would probably loop through the player character and/or use Humanoid:GetAccessories() to get all of this information during a Data Store save operation
local playerOutfit = [
    ["Shirt"] = ShirtID,
    ["Pants"] = PantsID,
    ["Accessory1"] = AccID,
    ...
]

And then on player join, you would look in that datastore to see if they have data, and if so, what that data is so you can load it onto them.

-- When the player joins a server, this would be done in a datastore load
-- Assuming you get the player earlier in your script
-- Clear their appearance so when you load in their saved appearance, it doesnt overlap (Accessories, etc)
player:ClearCharacterAppearance()

-- Then you would loop through the datastore's data and load all the IDs back onto the player
-- Assuming you get DataStoreService earlier
local playerOutfit = DataStoreService:GetDataStore("PlayerOutfits")
for key, value in ipairs(playerOutfit) do
    if key == "Shirt" or key == "Pants" then
        -- Assign their shirt or pants ID respectively, as these are not considered accessories
    else
        -- Use Humanoid:AddAccessory(AccID) to add the accessory properly to the player
    end
end

If you’re having trouble with data stores, I highly recommend checking out this article: Introduction to Saving Data

local players = game:GetService"Players"

local playersConnections = {}

local function onPlayerAdded(player)
	local function onCharacterRemoving(character)
		local function onPlayerRemoving(_player)
			if player == _player then
				if playersConnections[player] then
					if playersConnections[player].Connected then
						playersConnections[player]:Disconnect()
						playersConnections[player] = nil
					end
				end
				
				character.Archivable = true
				local characterClone = character:Clone()
				character.Parent = workspace
			end
		end
		
		if playersConnections[player] then return end
		playersConnections[player] = players.PlayerRemoving:Connect(onPlayerRemoving)
	end
	
	player.CharacterRemoving:Connect(onCharacterRemoving)
end

players.PlayerAdded:Connect(onPlayerAdded)

This seems to be working well on my end, tested with eight players in a test server.

A server or local script? and where would I need to place it?

You can put this wherever it’ll work fine.
Edit: I just fully read the post. This has to be a server script to work. Put in the serverscriptservice!

local PlayerService = game:service'Players'
PlayerService.PlayerRemoving:Connect(function(player)
local char = player.Character
local Cchar = char:Clone()
Cchar.Parent = game:service'ServerStorage'
end)

It’s not game:service, it’s game:GetService

https://developer.roblox.com/en-us/api-reference/function/ServiceProvider/service

That is deprecated, and for the better you should use game:GetService

Server script and wherever server scripts can execute.

Pretty sure Server scripts work in ServerScriptService and Workspace, right?

thats true but its shorter and I find it nicer on my eyes personally

This won’t work ingame because roblox deletes the character before they tell the server that the player leaves.

lol mb why is it saying this is a new topic