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

How do I create DataStores?

I understand how to retrieve things from Data Stores, thanks to the wiki, but I don't understand quite how to make one.

Say, for instance, I wanted to make a data store like this:

Store name: pointsTable "usr_"..player.PlayerId | Points

How exactly would I be able to create this? And is it possible to have multiple value columns like Points, Kills, Deaths, for example? Thank you.

1 answer

Okay first off yes you can store values in columns its called a table/array and you put those in the table. here is a sample code because I'm sure you understand how to do this and I don't really understand how to explain it. if you have any more questions feel free to PM me or put a comment so I can help you out some more.

--\\ Services //--
local datastoreservice = game:GetService('DataStoreService')
--\\ DataStores //--
local datastore = datastoreservice:GetDataStore('pointsTable')
--\\ Values //--
local sessiondata = {}
--\\ Functions //--
local function onPlayerAdded(player)
	local leaderstats = Instance.new('Folder')
	leaderstats.Name = 'leaderstats'
	local points = Instance.new('IntValue', leaderstats)
	points.Name = 'Points'
	local kills = Instance.new('IntValue', leaderstats)
	kills.Name = 'Kills'
	local deaths = Instance.new('IntValue', leaderstats)
	deaths.Name = 'Deaths'
	local key = ('user_'..tostring(player.UserId)) -- key, unique to the player
	local data = datastore:GetAsync(key) or { -- checks if they already have data saved and if not create a new data table
		Points 	= 0,
		Kills	= 0,
		Deaths	= 0,
	}
	-- put their stats in the values and make the data visible, if you dont want the stats visible just change the name of 'leaderstats' to something like 'Statistics' or 'Stats'
	points.Value 	= data.Points
	kills.Value 		= data.Kills
	deaths.Value 	= data.Deaths
	leaderstats.Parent = player
	sessiondata[key] = data -- store their data in another table
end
local function onPlayerRemoving(player)
	local leaderstats = player:WaitForChild('leaderstats')
	local points, kills, deaths = leaderstats:WaitForChild('Points'), leaderstats:WaitForChild('Kills'), leaderstats:WaitForChild('Deaths')
	local key = ('user_'..tostring(player.UserId)) -- key, unique to the player
	local data = sessiondata[key] -- get their data from the sessiondata table
	if data ~= nil then
		-- put their stats in the table and save to the datastore
		data.Points 	= points.Value
		data.Kills 		= kills.Value
		data.Deaths 	= death.Value
		local success, message = pcall(function() datastore:SetAsync(key, data) end) -- save the data
		if (success == false) then -- if the data didn't save properly then print a warning to the console
			warn('An error has occured: '..message)
		end
		sessondata[key] = nil -- remove the data, this isnt required but I like knowing that the table is as small as possible and only holds the data for all the players that are actually in the server.
	end
end
--\\ Run Code //--
for _, v in pairs (players:GetPlayers()) do onPlayerAdded(v) end -- if the script was restarted by console
players.PlayerAdded:connect(onPlayerAdded) -- connect to the onPlayerAdded function when the event is fired
players.PlayerRemoving:connect(onPlayerRemoving) -- connect to the onPlayerRemoving function when the event is fired

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