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

How to convert a DataStore model to a table?

So I read an idea on where you save a player's data as a model in ReplicatedStorage and put the values inside that model. Whenever the model changes then it converts into a table and saves those changes. The benefit of this is giving the data a visual appearance

----Question------ How would I go about converting the inside of the model to a table? I'm thinking somewhere along the lines of

Model.Changed:connect(function()
	for i,v in pairs(Model:GetChildren()) do
	v:UpdateAsync(player.UserId,v)
	end
end)

Is this the correct way?

1 answer

✓ Accepted answer

#Solution

Since there is no differentiation in the key that you save your data to, it is going to keep being overwritten until the last index in the Model:GetChildren() table.

I suggest you construct a dictionary, and save that :)


##Tips

Saving everytime the data is changed is a bit overkill - Just save when the player leaves.

And personally, I like to simply store all player data as descendants of the player object - especially since the in-game leaderboard format requires this. But that's just my preference..

  • Note: connect is deprecated, use Connect!

###Code

--Define datastore
local ds = game:GetService("DataStoreService"):GetDataStore("Stats")

--PlayerRemoving event
game.Players.PlayerRemoving:Connect(function(p)
	local data = {}; --Define array
	--Reference stats holder
	local Model = game.ServerStorage:FindFirstChild(p)
	for i,v in pairs(Model:GetChildren()) do
		data[v.Name] = v.Value --Add an index to the dictionary
	end
	ds:UpdateAsync(player.UserId,data)
end)

1 comment

0

Thank you so much!

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