How can I save attributes with dataStore?

Well, I already did that according to me the attributes and other IntValue values ​​are saved with tables, the problem is that it does not load or is that it does not load the Attributes (I don’t know which one of the two is, does it not load or not save?). the dataStore with the attributes just doesn’t work for me, or it doesn’t load it

local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
--Money
local MoneyData = datastore:GetDataStore("MoneyData")
--NeedPlayers
local NeedsDataService = datastore:GetDataStore("NeedsData")

players.PlayerAdded:connect(function(plr)
	--Folders
	local Folder = Instance.new("Folder")
	Folder.Name = "DataPlayer"
	Folder.Parent = plr
	--NeedPlayers
	local NeedsPlayerStats = Instance.new("Folder")
	NeedsPlayerStats.Name = "NeedsPlayerStats"
	NeedsPlayerStats.Parent = Folder
	--Money
	local Dinero = Instance.new("IntValue")
	Dinero.Name = "Money"
	Dinero.Value = MoneyData:GetAsync(plr.UserId) or 0
	Dinero.Parent = plr
	local NeedsPlayersInstance = {
		{type = "IntValue", name = "Hunger"},
		{type = "IntValue", name = "Thirst"},
	}
	for _, value in pairs(NeedsPlayersInstance) do
		local val = Instance.new(value.type)
		val.Name = value.name
		val.Parent = NeedsPlayerStats
		val:SetAttribute("Max_needs_food", 0)
	end
	
	local Needs_Data_Final = nil
	local success, err = pcall(function()
		Needs_Data_Final = NeedsDataService:GetAsync(plr.UserId)
	end)

	--NeedsPlayer
	if success and Needs_Data_Final ~= nil then
		NeedsPlayerStats:WaitForChild("Hunger").Value = Needs_Data_Final.Hunger
		NeedsPlayerStats:WaitForChild("Thirst").Value = Needs_Data_Final.Thirst
		NeedsPlayerStats:WaitForChild("Hunger"):GetAttribute("Max_needs_food", Needs_Data_Final.Max_Food_Hunger)
		NeedsPlayerStats:WaitForChild("Thirst"):GetAttribute("Max_needs_food", Needs_Data_Final.Max_Food_Thirst)
	else
		NeedsPlayerStats:WaitForChild("Hunger"):GetAttribute("Max_needs_food", 100)
		NeedsPlayerStats:WaitForChild("Thirst"):GetAttribute("Max_needs_food", 100)
		NeedsPlayerStats:WaitForChild("Hunger").Value = 100
		NeedsPlayerStats:WaitForChild("Thirst").Value = 100
	end
end)

players.PlayerRemoving:Connect(function(plr)
	--Money
	MoneyData:SetAsync(plr.UserId, plr.Money.Value)
	--NeedsPlayer
	local NeedsPlayer = {
		Hunger = plr.DataPlayer.NeedsPlayerStats.Hunger.Value;
		Thirst = plr.DataPlayer.NeedsPlayerStats.Thirst.Value;
		Max_Food_Hunger = plr.DataPlayer.NeedsPlayerStats.Hunger:GetAttribute("Max_needs_food");
		Max_Food_Thirst = plr.DataPlayer.NeedsPlayerStats.Thirst:GetAttribute("Max_needs_food");
	}

	local Success, err = pcall(function()
		NeedsDataService:SetAsync(plr.UserId, NeedsPlayer)
	end)

	if Success then
		print("Se ha guardado correctamente / Has been saved successfully")
	else
		print("hubo un error al guardar / there was an error saving")
		warn(err)
	end
end)
1 Like

The same way you save any other value. Attributes just allow you to attach values onto an instance so as long as they’re serialisable then they can be saved. This is probably not your question.

I’m obviously not going to do a deep code review since you just threw your entire code into the thread but with a brief skim I noticed that you might be confusing the ways API are used. Remember to check out documentation to see how functions are used; GetAttribute only takes one argument, the name of the attribute whose value you want to retrieve. You probably intended to use SetAttribute instead.

5 Likes