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

DataStore That Saves Hats Does Not Work?

I have a folder located within the player where it is used to store hats the player gets. I made this script and put it into workspace (also tried ServerScriptStorage) both didn't work. The script is a regular script...

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("HatSave")
	 
game.Players.PlayerAdded:Connect(function(player)
	   local hats = player:WaitForChild("Hats"):GetChildren()
	 
	    for _, child in pairs(hats) do
	        child.Parent = ds:GetAsync(player.UserId)
        ds:SetAsync(player.UserId, child.Parent)
	    end
end)
	 
	 
game.Players.PlayerRemoving:Connect(function(player)
	    local hats = player.Hats:GetChildren()
	 
	    for _, child in pairs(hats) do
	    ds:SetAsync(player.UserId, child.Parent)
	    end
	end)

I don't know how efficient this method is, but feedback is always good

1 comment

0

You can't save Instances, you can only save Strings, Numbers, and Booleans.

1 answer

Your method is inefficient. What you could do is make an array, put all the hats in the array, then make the DataStoreService save the data.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("HatSave")

game.Players.PlayerAdded:Connect(function(player)
	--We'll just print the hats saved

	local data = dss:GetAsync(player.UserId) or {}

	for _,v in pairs(data) do
		print(v.Name)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local hatschildren = player:WaitForChild('Hats'):GetChildren()
	local hats = {}

	for _,v in pairs(hatschildren) do
		hats[#hats+1] = v.Name
	end

	-- Your error handler
	local success,message = pcall(function()
		ds:SetAsync(player.UserId, hats)
	end)
	if success then
		print("Successfully saved "..player.Name.."'s data")
	else
		warn("Failed to save "..player.Name.."'s data. :"..message)
	end
end)

If there's any errors, please mention

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