So i Made a Hat Shop, but idk how i can save the content of the OwnedHats Folder in My DSS

So i Made a Hat Shop, but idk how i can save the content of the OwnedHats Folder in My DSS, this the datastore script :

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("TestSave1")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local fp = Instance.new("IntValue")
	fp.Name = "Punch"
	fp.Parent = leaderstats

	local OwnedHats = Instance.new("Folder")
	OwnedHats.Name = "OwnedHats"
	OwnedHats.Parent = player
	

	local EquippedHat = Instance.new("StringValue")
	EquippedHat.Name = "EquippedHat"
	EquippedHat.Parent = player

	local data1
	local data2
	local data3
	local success, errormessage = pcall(function()
		data1 = dataStore:GetAsync(player.UserId.."Punch")
		data2 = dataStore:GetAsync(player.UserId.."OwnedHats")
		data3 = dataStore:GetAsync(player.UserId.."EquippedHat")

	end)

	if success then
		fp.Value = data1
		OwnedHats = data2
		EquippedHat = data3
	else
		print("There was an error.")
		warn(errormessage)
	end
	

end)

game.Players.PlayerRemoving:Connect(function(plr)

	local success, errormessage = pcall(function()
		dataStore:SetAsync(plr.UserId.."Punch", plr.leaderstats.Punch.Value)
		
		
		local tbl = {}
		for _,v in pairs(plr.OwnedHats:GetChildren()) do
			tbl[#tbl+1] = v.Name
		end
		dataStore:SetAsync(plr.UserId, tbl) -- saving the data
		
		dataStore:SetAsync(plr.UserId.."EquippedHat", plr.EquippedHat.Value)
	end)

	if success then
		print("Successfully saved Player Data!")
	else
		print("There was an error while saving player data!")
		warn(errormessage)
	end
end)

I already try somthing but nothing happen

3 Likes

This code, should work. I don’t see why it shouldn’t.

Here are a few suggestions and questions.

  1. If there is not success in getting the data, why do you not assign some default values?
  2. Can I have a view of the explorer, or what the object for a “Hat” is?

Here’s how you can roughly encode and decode the contents of the folder which should contain accessories.

Assuming you have a folder with all the hats consistently in the game.

local Folder = -- path to owned hats folder
local Hats = -- path to existing hat folder

local function Encode()
	local EncodedData = {}
	for idx, hat in pairs(Folder:GetChildren()) do
		EncodedData[idx] = hat
	end
	return EncodedData
end

local function Decode(EncodedData)
	for idx, hat in pairs(Hats:GetChildren())
		if table.find(EncodedData,hat.Name) then
			hat:Clone().Parnet = Folder
		end
	end
end

My DS say “Successfully saved Player Data!” but nothing save lol

Yes this is the screenshot of the Replicated Storage

Already try but nothing happen

May I ask why are you setting everything individually? why not just do:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("TestSave1")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local fp = Instance.new("IntValue")
	fp.Name = "Punch"
	fp.Parent = leaderstats

	local OwnedHats = Instance.new("Folder")
	OwnedHats.Name = "OwnedHats"
	OwnedHats.Parent = player
	

	local EquippedHat = Instance.new("StringValue")
	EquippedHat.Name = "EquippedHat"
	EquippedHat.Parent = player

	local data
	local success, errormessage = pcall(function()
		data1 = dataStore:GetAsync(player.UserId)

	end)

	if success then
		fp.Value = data.Punch
		OwnedHats = data.OwnedHats
		EquippedHat = data.EquippedHat
	else
		print("There was an error.")
		warn(errormessage)
	end
	

end)

game.Players.PlayerRemoving:Connect(function(plr)

	local success, errormessage = pcall(function()
		local tbl = {}
		for _,v in pairs(plr.OwnedHats:GetChildren()) do
			tbl[#tbl+1] = v.Name
		end
		local data = {OwnedHats = tbl, 
		EquippedHat = plr.EquippedHat.Value, 
		Punch = plr.leaderstats.Punch.Value}
		dataStore:SetAsync(plr.UserId, data)
	end)

	if success then
		print("Successfully saved Player Data!")
	else
		print("There was an error while saving player data!")
		warn(errormessage)
	end
end)

This is not work i think, look this script :

HatShopServer.lua

local replicatedStorage = game:GetService("ReplicatedStorage")
local Hats = replicatedStorage.Hats

local currency = "Punch"

replicatedStorage.BuyHat.OnServerInvoke = function(player, hatName)
	local hat = Hats:FindFirstChild(hatName)

	if Hats:FindFirstChild(hatName) then
		if not player.OwnedHats:FindFirstChild(hatName) and player.EquippedHat.Value ~= hatName then
			if player.leaderstats[currency].Value >= hat.Price.Value and player.Character ~= nil  then
				player.leaderstats[currency].Value -= hat.Price.Value
				for i, v in pairs(player.Character:GetChildren()) do
					if v.Name == "CurrentHat" then
						v:Destroy()
					end
				end
				local clonedHat = hat:FindFirstChild("HatMesh"):Clone()
				clonedHat.Name = "CurrentHat"
				clonedHat.Parent = player.Character
				local val = Instance.new("StringValue")
				val.Name = hatName
				val.Parent = player.OwnedHats
				player.EquippedHat.Value = hatName
				return "Bought"
			else
				return false
			end
		elseif player.OwnedHats:FindFirstChild(hatName) and player.EquippedHat.Value ~= hatName then
			for i, v in pairs(player.Character:GetChildren()) do
				if v.Name == "CurrentHat" then
					v:Destroy()
				end
			end
			local clonedHat = hat:FindFirstChild("HatMesh"):Clone()
			clonedHat.Name = "CurrentHat"
			clonedHat.Parent = player.Character
			local val = Instance.new("StringValue")
			val.Name = hatName
			val.Parent = player.OwnedHats
			player.EquippedHat.Value = hatName
			return "Equipped"
		elseif player.OwnedHats:FindFirstChild(hatName) and player.EquippedHat.Value == hatName then
			for i, v in pairs(player.Character:GetChildren()) do
				if v.Name == "CurrentHat" then
					v:Destroy()
				end
			end
			player.EquippedHat.Value = ""
			return "Equip"
		end
	end
end

this is how the server hat work

you should save the hats to the datastore by storing each of them names in a table, and then to load loop through the hats and clone from replicatedstorage, make sure all the hats are loaded to the player before saving them too so data doesnt get lost when the player leaves too early.
also i strongly recommend using profileservice

Do you have an Example of the script ?? LOL

So I do this :

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("TestSave1")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local fp = Instance.new("IntValue")
	fp.Name = "Punch"
	fp.Parent = leaderstats



	local OwnedHats = Instance.new("Folder")
	OwnedHats.Name = "OwnedHats" 
	OwnedHats.Parent = player


	local EquippedHat = Instance.new("StringValue")
	EquippedHat.Name = "EquippedHat"
	EquippedHat.Parent = player
	
	local Data
	local data1
	local data3
	local success, errormessage = pcall(function()
		data1 = dataStore:GetAsync(player.UserId.."Punch")
		
		Data = dataStore:GetAsync(player.UserId.."OwnedHats")
		for _,Item in ipairs(Data) do
			print(Item)
			local Value	= Instance.new("StringValue",OwnedHats)
			Value.Name = Item
		end
		
		data3 = dataStore:GetAsync(player.UserId.."EquippedHat")

	end)

	if success then
		print("Successfully Saved Data Store!")
	else
		print("There was an error.")
		warn(errormessage)
	end
	
	player.CharacterAdded:Connect(function()
		for i,accessory in pairs(OwnedHats:GetChildren()) do
			game.ServerStorage.AllAccessories[accessory.Name]:Clone().Parent = player.Character
		end

	end)


end)


game.Players.PlayerRemoving:Connect(function(plr)

	local success, errormessage = pcall(function()
		
		local Accessories = {}

		for _,Item in pairs(plr.OwnedHats:GetChildren()) do
			print(Item.Name)
			table.insert(Accessories,Item.Name)
		end

		dataStore:SetAsync(plr.UserId.."OwnedHats",Accessories)

		dataStore:SetAsync(plr.UserId, plr.leaderstats.Punch.Value)
	end)

	if success then
		print("Successfully saved Player Data!")
	else
		print("There was an error while saving player data!")
		warn(errormessage)
	end
end)

But i now got this error :