How could i make the player.Character still accesible after a player leaves?

im trying to make a data save
but this entire script breaks at

	local essence = plr.Character:FindFirstChild("Head").essence

i cannot acces the “plr.Character”

How could i be able to acces it?

local function save(plr)
	local items = {}
	
	local essence = plr.Character:FindFirstChild("Head").essence
	local function RGBstringToColor3(RGBstring)
return tostring(RGBstring:ToHex())
	end
	local essencecolor = RGBstringToColor3(essence.BillBoardGui.Essence.TextColor3)

local essencesave =  {
		EssenceName = essence.BillBoardGui.Essence.Text,
		EssenceColor = essencecolor,
		EssenceFont = essence.BillBoardGui.Essence.Font.Name, 
		Chance = essence.chance.BillBoardGui.Chance.Text,
	}
	local backpack = plr.Backpack

	for i, item in backpack:GetChildren() do
		table.insert(items, item.Name)
		print(item.Name)
	end

	for i, itemers in ipairs(items) do
		print(itemers)
	end
	local datatobesaved = {
		Rolls = plr.leaderstats.Rolls.Value,
		ItemTable = items,
		CurrentEssence = essencesave,
		
	}

	local playerid = "Player_" .. plr.UserId

	local success, err = pcall(function()
		rollstore:UpdateAsync(playerid, function(old)
			for i, v in pairs(datatobesaved) do
				if i == nil or v == nil then
					datatobesaved = old
					print('datasave = old')
				end
			end

			--you can add individual checks to make sure each data element
			--is there and ready to be saved.

			return datatobesaved
		end)
	end)

	if success then
		print("data saved! (server)")
	else
		print("data failed to save!")
	end
end
players.PlayerRemoving:Connect(save)

1 Like

I think its because the player gets removed before the character can still exist so what I mean is that if the player is gone the character too.

1 Like

yes i know this, but how could i make it so i can still access the plr.Character?

1 Like

Clone the character on PlayerRemoving. The plr.Character cannot exist without the player as you are refrencing the player first.

To clone the character, you have to set it’s Archivable property to true.

You cannot access the player Character, because the player is gone, it’s impossible. If you want to get the character after the player leaves, just clone it before they leave, and make sure Archivable Is false, like @TimeFrenzied said.

Could i have a sample of code by what u mean i am not sure to understand

game.Players.PlayersRemoving:Connect(function(plr))
    local clone = plr.Character:Clone()
    clone.Name = plr.Name
    clone.Parent = game.ServerStorage
end)

That’s what the previous post meant I think

Also could you state why you need to keep the players character after they leave? Sometimes there may be a simpler solution to solve your goal

sadly your code doesnt work
i want the text on top of ur head to save with its color, text, etc
(which is located in the plr.Character as it is a billboardgui)

game.Players.PlayerRemoving:Connect(function(plr)
local clone = plr.Character:Clone()
clone.Name = plr.Name
clone.Parent = game.ServerStorage.PlayerClone
end)

It thinks the clone is nil as you haven’t set the archivable property to true.

plr.Character.Archivable = true

i did tho
this script is when they join

local players = game:GetService("Players")

local function load(plr)

	local folder = Instance.new("Folder", plr)
	local character = plr.CharacterAdded:Wait()
--Archive
	plr.Archivable = true
	character.Archivable = true
--Archive
	folder.Name = 'leaderstats'
	local rolls = Instance.new("IntValue", folder)
	rolls.Name = 'Rolls'
	rolls.Value = 0

	local luckmultiplier = Instance.new("IntValue", folder)
	luckmultiplier.Name = 'Luck'
	luckmultiplier.Value = 1
	
	local playerid = "Player_" .. plr.UserId
	local data
	local success, err = pcall(function()
		data = rollstore:GetAsync(playerid)
	end)
	if data then
		if data['Rolls'] then
			rolls.Value = data['Rolls']
		else
			rolls.Value = 0
		end

		if data['ItemTable'] then
			print('ItemTable is not nil')
			for i,v in ipairs(data['ItemTable']) do
				local foods = game.ReplicatedStorage.food
				local tools = foods.tools
				print(v .. ' is in itemtable.')

				for i, tool in pairs(tools:GetChildren()) do
					if tool.Name == v then
						print('creating tool')


						newtool = tool:Clone()
						newtool.Name = v
						newtool.Handle.Anchored = false
						newtool.Parent = plr.Backpack
						module.ItemUse(newtool.Handle:FindFirstChild("LuckBooster").Value, newtool, newtool.Handle:FindFirstChild('Time').Value, plr, v)

					else
						print('nope')
					end
				end
			end

		else
			print('its nil rn')
		end
		
		if data['CurrentEssence'] then
			local essencemodule = require(script.ModuleScript)
			local essence =  character.Head:WaitForChild('essence')
			local name = data['CurrentEssence']['EssenceName']
			print(name)
			essencemodule.Configure(data['CurrentEssence'], essence)
		end
	else
		rolls.Value = 0
	end
	
	
	
	
	for i, theitems in pairs(plr.Backpack:GetChildren()) do
		print(theitems.Name .. 'ARE IN THE BACKPACK' )
	end
--	playerclonefr = plr.Character:Clone()
	
end

players.PlayerAdded:Connect(load)