How does one save a character's hat?

So I’ve spent 4 days just trying to wrap my head around data saving a characters appearence in Roblox, but I haven’t gotten much help. I finally completed my character customizer UI, but i don’t want player to re customize their character everytime they join. *note that the hats are not from Roblox , I made the accessories my self. I resorted to saving hats because i just feel like im not capable of saving other things. I’ve almost looked at every forum about data saving hats. This is what I have so far.

Blockquote
local dss = game:GetService(“DataStoreService”)
local myDataStore = dss:GetDataStore(“Outfit”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

1 Like

Saving:
pcall(function() myDataStore:SetAsync(Player.UserId..“hat”, ID OF HAT) end)
Retrieving:
local s, hat = pcall(function() return myDataStore:GetAsync(Player.UserId..“hat”) end) if s then —code that equips hat end

this doesn’t help at all. your basically giving me a script. I’ve heard that you have to serialize your hats using a string value that is placed the hat.

On mobile so was hoping to give an easy answer, but that’s fair. It is indeed true that you cannot save your hat simply by saving the “hat.” Therefore, what the code above tries to have you do is insert the hat’s unique ID into the save script so that you can successfully save the hat. When the “hat” is retrieved with GetAsync, it returns hat ID. In order to find the hat ID from a script, it should be somewhere inside the properties of the “accessory” you are trying to save.

1 Like

Alright I will see if I can find the id for that hat

1 Like

Dear fxding

I would say get a local desktop copy of your hat file using blender or other 3D modelling tools and try to move/ copy your chat from Desktop on to Roblox studio i would suggest viewing the properties in a more depth and to give more information. Kindly read this post and see if this solves your problem in any way. Thank you

Yours Sincerely
@DentinDev

@DentinDev I don’t need help on making accessories… I need help on saving them.

@fxding_cam Ill try to resolve the bug by working on your error.
Kindly give me sometime. Thank you.

@DentinDev Its not an error, its what i am trying to figure out. :man_facepalming:

@Chatowillwin also I can’t seem to find the ID of an accessory. Remember what I said, these are not roblox made accessories. When I click on the accessory I cant find anything in the properties tab saying Accessory ID

Make a folder in each player called “Items” and whenever a player equips an item then a new String Value is created with the same name of the equipped item.

--DEFINED "OURPLAYER" ON YOUR OWN
local function onEquip(item)
   local Value = Instance.new("StringValue",OurPlayer.Items)
   Value.Name = item.Name
end

Try making a folder in the ServerStorage called “Everything” and place every equipable accessory on your game inside the folder, so once the player leaves the game.

local function onPlayerLeave(plr)
  local Wear = {} --Table of everything our character wore
  for _,item in pairs(plr.Items) do
      table.insert(Wear,item.Name) --Insert the names of the equipped item inside a table so we can use it next time the player joins
  end
  pcall(function()
     MyDataStore:SetAsync(plr.UserId,Wear)--were saving the table
  end)

Now how do we load this once the player joins back?

local function onPlayerAdded(plr)
  local data --Make a variable called data
  local Folder = Instance.new("Folder",plr) --Make the items folder
  Folder.Name = "Items"
  pcall(function()
  data = MyDataStore:GetAsync(plr.UserId) --data is a table since we saved a table when the player left
  end)
  for _,item in pairs(data) do --were looping  through the table
       local Value = Instance.new("StringValue",Folder)
       Value.Name = item
  end
   plr.CharacterAdded:Connect(function()
      for i,accessory in pairs(Folder) do
          game.ServerStorage.Everything[accessory.Name]:Clone().Parent = plr.Character -- were looping through our folder and get everything in the everything folder inside our character
      end
   end)
end

You can just remove them from the Items folder everytime they unequip.

1 Like

@OctaLua Ok, thank you so much for elaborating everything. :grinning: :+1:
I will look over it.

1 Like

@OctaLua umm I am having a little bit of trouble here. The script is saying that data isn’t a table take a look what I have put together.

local dss = game:GetService("DataStoreService")
local myDataStore = dss:GetDataStore("Clothes")

game.Players.PlayerRemoving:Connect(function(plr)
	local Wear = {}
	for _,item in pairs(plr.Items) do
		table.insert(Wear,item.Name)
	end
	pcall(function()
		myDataStore:SetAsync(plr.UserId,Wear)
	end)
end)

game.Players.PlayerAdded:Connect(function(plr)
	local data
	local Folder = Instance.new("Folder",plr)
	Folder.Name = "Items"
	pcall(function()
		data = myDataStore:GetAsync(plr.UserId)
	end)
	for _,item in pairs(data) do
		local Value	= Instance.new("StringValue",Folder)
		Value.Name = item
	end
	plr.CharacterAdded:Connect(function()
		for i, accessory in pairs(Folder) do
			game.ServerStorage.Everything[accessory.Name]:Clone().Parent = plr.Character
		end
	end)
end)
game.Players.PlayerAdded:Connect(function(plr)
	local data
	local Folder = Instance.new("Folder",plr)
	Folder.Name = "Items"
	pcall(function()
		data = myDataStore:GetAsync(plr.UserId)
	for _,item in pairs(data) do
		local Value	= Instance.new("StringValue",Folder)
		Value.Name = item
	end
	plr.CharacterAdded:Connect(function()
		for i, accessory in pairs(Folder) do
			game.ServerStorage.Everything[accessory.Name]:Clone().Parent = plr.Character
		end
	end)
  end)
end)

Uh try this, we wrap them all inside a pcall to not error new players.

same thing is happening for player removing, so i suppose i wrap it in a pcall also?

Edit: Yeah its just not working correctly.

Yes, try wrapping both the for loop and wear table inside a pcall of player removing

    pcall(function()
	local Wear = {}
	for _,item in pairs(plr.Items) do
		table.insert(Wear,item.Name)
	end
		myDataStore:SetAsync(plr.UserId,Wear)
	end)

@OctaLua yep nothing is working. But thanks for the help! :slightly_smiling_face: :+1:
I will figure it out.

I think the problem lies here since last time we I didnt do get children, here is the new edited one.

	plr.CharacterAdded:Connect(function()
		for i, accessory in pairs(Folder:GetChildren()) do
			game.ServerStorage.Everything[accessory.Name]:Clone().Parent = plr.Character
		end
	end)

@OctaLua nope still nothing there is also nothing in the output.
Its ok though

some one told me this. I will quote what they said

“Have you ensured the data exists yet? The data variable is clearly nil at line 15 so if the GetAsync() returned nil (no data found) then it would remain nil and error.”