Best way to save data store

im wondering whats the best way to save multiple datastores at once without yeild or cooldown? eg character creation data store

2 Likes

Use a module datastore, like profile service, note that there are newer ones that use better features but PS is the most widely used one. This is sadly only one single datastore, however, I kind of wondering about your usecase? Are you trying to update a variable in two different datastores at the same time? This seems VERY interesting. Because I use PS with 1 datastore, but easily have many different stores inside of that holding all kinds of tables and all kinds of variables. I also use getsortedmap.

2 Likes

Use either table ordereddatastores then or separate keys that way u use 1 datastore but can save multiple variables and types

1 Like

how would i save a bool value and a string value in a table and then save that into datastore?

something like

local table = {
["bool"] = true,
["string"] = "your string"
}

for _, valuename in table
    ordereddatastorename:setasync(key, valuename, value)
end

obviously replace variable names and stuff etc, also this was made in like 1 min bc i am busy so i prob made a mistake somewhere but that’s the gist

-hope that helps

If i was creating this, i would not trust roblox at certain point, i would use Firebase, Supabase, MySQL, Postgres or any other alternatives, using a external node.js (or docker) server

you could do a default table with all in default hats, shirts and pants, for example

local defaultcharacter = {
 ["Shirt"] = 123456789,
 ["Pants"] = 987654321,
 ["Accessories"] = {
  Hair = 1325476980,
  -- Include others
 }
}

and using this as reference, then you could do a game table (that includes everyone) and you could change, add, delete something at any time without errors and head pain

when they leave, you just get the player table inside the game table and save it

heres a little demonstration:

local datastore = game:GetService("DataStoreService")
local charactersdata = datastore:GetDataStore("Character")

local players = {}

local defaultcharacter = {
	["UserID"] = 1,
	["Shirt"] = 123456789,
	["Pants"] = 987654321,
	["Accessories"] = {
		Hair = 1325476980,
		-- Include others
	}
}

game.Players.PlayerAdded:Connect(function(plr)
	local success, result = pcall(function()
		return charactersdata:GetAsync("Person_"..plr.UserId)
	end)
	if success then
		if result then
			table.insert(players, result)
		else
			local datatoinsert = defaultcharacter
			datatoinsert.UserID = plr.UserId
			table.insert(players, datatoinsert)
			return			
		end
	else
		error("Error: "..result)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, result = pcall(function()
		for i,v in ipairs(players) do
			if v.UserID == plr.UserId then
				return charactersdata:SetAsync("Person_"..plr.UserId, v)				
			end
		end
	end)	
	
	if not success then
		error("Error: "..result)		
	end	
end)

with this, you are saving the entire player character, and you just need to adjust to apply this in game

hope i helped you

1 Like

Hey, i forgot to say that you need to REMOVE the player table from the game table

a little change:

local datastore = game:GetService("DataStoreService")
local charactersdata = datastore:GetDataStore("Character")

local players = {}

local defaultcharacter = {
	["UserID"] = 1,
	["Shirt"] = 123456789,
	["Pants"] = 987654321,
	["Accessories"] = {
		Hair = 1325476980,
		-- Include others
	}
}

game.Players.PlayerAdded:Connect(function(plr)
	local success, result = pcall(function()
		return charactersdata:GetAsync("Person_"..plr.UserId)
	end)
	if success then
		if result then
			table.insert(players, result)
		else
			local datatoinsert = defaultcharacter
			datatoinsert.UserID = plr.UserId
			table.insert(players, datatoinsert)
			return			
		end
	else
		error("Error: "..result)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, result = pcall(function()
		for i,v in ipairs(players) do
			if v.UserID == plr.UserId then
				return charactersdata:SetAsync("Person_"..plr.UserId, v)				
			end
		end
	end)	
	for i,v in ipairs(players) do
		if v.UserID == plr.UserId then
			table.remove(players, i)		
		end
	end
	if not success then
		error("Error: "..result)		
	end	
end)

i added

for i,v in ipairs(players) do
	if v.UserID == plr.UserId then
		table.remove(players, i)		
	end
end

so the player isnt on the game table anymore.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.