Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Manipulating the Character upon Player.Removing?

Hello,

I'm aware that the Character of a Player Object is removed faster than the function joined to a Player.PlayerRemoving signal can call. I'm helping develop a Tool Persistence Script for someone here and I was wondering if there were any ways to work around or compensate for this issue from the perspective of a ServerScript?

local DataStoreService = game:GetService("DataStoreService")
local ToolData = DataStoreService:GetDataStore("ToolData")

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

local function SaveTools(Player)
	if not (Player) then return end
	local Backpack = Player:FindFirstChild("Backpack")
	--// Cannot be called because the Character is missing
	local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
	if (Humanoid) then Humanoid:UnequipTools()
		local ToolsToSave = {}
		for _,Tool in pairs(Backpack:GetChildren()) do
			if (Tool:IsA("Tool") and ServerStorage.Tools[Tool]) then
				table.insert(ToolsToSave, Tool.Name)
			end
		end
		if (#ToolsToSave < 1) then return end
		ToolData:SetAsync(Player.UserId, ToolsToSave)
	end
end

Players.PlayerRemoving:Connect(SaveTools)

1 answer

Hmm, this is quite interesting.. In such case, I would keep track of the tools that go into a player's backpack. You can do this via an array, or just list of ObjectValues, or a StringValue with JSON formatted string made via Http:JSONEncode(). removing the tools once they are being removed from the player's backpack

This discussion is preserved as it was. New answers, comments, and votes are closed.