Shirt template datastore not working

i have a custom character creation where u can change ur shirt and pants and when u press back it saves
but when i press back and rejoin i dont have a shirt or pants
my script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local SaveDataEvent = game.ReplicatedStorage:WaitForChild("SaveData")

local ClothingSave = DataStoreService:GetDataStore("ClothingSave")

SaveDataEvent.OnServerEvent:Connect(function(Plr)
	Plr.CharacterAdded:Connect(function(Chr)
	local information = {
      ["Shirt"] = Chr.Shirt.ShirtTemplate;
      ["Pants"] = Chr.Pants.PantsTemplate;
      }
      ClothingSave:SetAsync(Plr.UserId, information)	
	end)
	
end)

game.Players.PlayerAdded:Connect(function(Plr)
	Plr.CharacterAdded:Connect(function(Chr)
	local good, info = pcall(function()
		return(ClothingSave:GetAsync(Plr.UserId))
	end)
	Chr:WaitForChild("Shirt").ShirtTemplate = info.Shirt
		Chr:WaitForChild("Pants").PantsTemplate = info.Pants	
	end)
end)

Not enough information. How exactly are you testing this? You need to do some debugging and provide more information than “it doesn’t work, here’s my code”. See: Debugging.

When the SaveDataEvent is fired by the client, it will create a new connection on the player that will only save the shirt and pants templates when the character respawns and that’s under the assumption that a Shirt and Pants will exist in the character (if they are nil this script will error because your code assumes they exist when they might not).

2 Likes

im testing this by commiting my script and publishing to roblox

local players = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local dataStore = dataStores:GetDataStore("DataStore")

local playerClothing = {}

local function onPlayerAdded(player)
	local function onCharacterAppearanceLoaded(character)
		local shirt = character:FindFirstChildOfClass("Shirt")
		if shirt then
			shirt:Destroy()
		end
		
		local pants = character:FindFirstChildOfClass("Pants")
		if pants then
			pants:Destroy()
		end
		
		local shirtGraphic = character:FindFirstChildOfClass("ShirtGraphic")
		if shirtGraphic then
			shirtGraphic:Destroy()
		end
		
		shirt = Instance.new("Shirt")
		shirt.ShirtTemplate = playerClothing[player]["Shirt"] or ""
		shirt.Parent = character
		
		pants = Instance.new("Pants")
		pants.PantsTemplate = playerClothing[player]["Pants"] or ""
		pants.Parent = character
		
		shirtGraphic = Instance.new("ShirtGraphic")
		shirtGraphic.Graphic = playerClothing[player]["ShirtGraphic"] or ""
		shirtGraphic.Parent = character
	end
	
	player.CharacterAppearanceLoaded:Connect(onCharacterAppearanceLoaded)
	
	playerClothing[player] = {}
	local success, result = pcall(function()
		return dataStore:GetAsync("Clothing_"..player.UserId)
	end)
	
	if success then
		if result then
			if type(result) == "table" then
				playerClothing[player] = result
			end
		end
	else
		warn(result)
	end
end

local function onPlayerRemoving(player)
	local character = player.Character
	if character then
		local clothing = {}
		
		local shirt = character:FindFirstChildOfClass("Shirt")
		if shirt then
			clothing["Shirt"] = shirt.ShirtTemplate
		end

		local pants = character:FindFirstChildOfClass("Pants")
		if pants then
			clothing["Pants"] = pants.PantsTemplate
		end

		local shirtGraphic = character:FindFirstChildOfClass("ShirtGraphic")
		if shirtGraphic then
			clothing["ShirtGraphic"] = shirtGraphic.Graphic
		end
		
		local success, result = pcall(function()
			return dataStore:SetAsync("Clothing_"..player.UserId, clothing)
		end)
		
		if success then
			if result then
				print(result)
			end
		else
			warn(result)
		end
	end
end

local function onServerShutdown()
	for _, player in ipairs(players:GetPlayers()) do
		local character = player.Character
		if character then
			local clothing = {}

			local shirt = character:FindFirstChildOfClass("Shirt")
			if shirt then
				clothing["Shirt"] = shirt.ShirtTemplate
				print(shirt.ShirtTemplate)
				print(playerClothing[player]["Shirt"])
			end

			local pants = character:FindFirstChildOfClass("Pants")
			if pants then
				clothing["Pants"] = pants.PantsTemplate
			end

			local shirtGraphic = character:FindFirstChildOfClass("ShirtGraphic")
			if shirtGraphic then
				clothing["ShirtGraphic"] = shirtGraphic.Graphic
			end

			local success, result = pcall(function()
				return dataStore:SetAsync("Clothing_"..player.UserId, clothing)
			end)

			if success then
				if result then
					print(result)
				end
			else
				warn(result)
			end
		end
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(onServerShutdown)

Just wrote up this system which saves a player’s last clothing before they leave, when they rejoin the clothing is loaded and cached in a dictionary, after this, each time their character is loaded their avatar’s clothing is removed and their cached clothing is added.