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

[SOLVED] Multiple keys when using two DataStores in one script?

I am making a game using DataStores and I want to use more than when DataStore to save different things. For Example: Have one DataStore for the players cash, xp, and level and one for the players health, walkspeed, and last CFrame (To load the players position when they join back on the game). My question is when using multiple DataStores do I need more than one key variable? Example:

local DataStoreService = game:GetService("DataStoreService")
local DataStoreCXL = DataStoreService:GetDataStore("CashXPLevel")
local DataStoreHWLC = DataStoreService:GetDataStore("HealthWalkSpeedLastCFrame")

local function Load(plr)
	local key = "plr-"..plr.UserId
	local key2 = "plr-"plr.UserId -- do I need this key?

	local Stats1
	local Stats2

	pcall(function()
		Stats1 = DataStoreCXL:GetAsync(key)
		Stats2 = DataStoreHWLC:GetAsync(key2)
	end)

	if Stats1 then
		-- Load data
	else
		-- More code
	end

	if Stats2 then
		-- Load data
	else
		-- More code
	end
end

local function Save(plr)
	local key = "plr-"..plr.UserId -- the first key
	local key2 = "plr-"..plr.UserId -- second key

	pcall(function()
		DataStoreCXL:SetAsync(key, SomeTable) -- Save the game stats
		DataStoreHWLC:SetAsync(key2, SomeOtherTable) -- Save the players stats
	end)	
end

game.Players.PlayerAdded:Connect(Load)
game.Players.PlayerRemoving:Connect(Save)

I know the example is not in a working state it was just to show my idea.

Thanks for helping!

8 comments

0

You don't need to use separate data stores at all! You can save tables, the table will contain all values.

0

But if I do want to use two data stores do I need another key?

0

variable

0

You should only need one key, of course you will still need Stats1 & Stats2 (the keys are referencing the same thing anyway)

0

Thanks If you post that as a answer I will accept it! (:

0

you mean me?

0

Yes

0

hmm okay

1 answer

✓ Accepted answer

You should only need one key, of course you will still need Stats1 & Stats2 (the keys are referencing the same thing anyway)

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