Combining Datastores / More keys

Hello!
Right now I have 2 different datastores (for Playerstats and Weapons)
But I’m planning to add a third datastore for settings, so I’m want to combine these datastores into one, so I only have to call the datastore once.

These are my 2 scripts rightnow:
Playerstats:

local function loadPlayerData(player)
	local userId = player.UserId
	local success, data = pcall(function()
		return PlayerDataStore:GetAsync(tostring(userId))
	end)

	if success then
		if data then
			playerData[userId] = data
			--player.leaderstats.K.Value = data.Kills
			--player.leaderstats.D.Value = data.Deaths
		else
			playerData[userId] = {Kills = 0, Deaths = 0, Level = 1, Xp = 0}
		end
	else
		warn("Failed to retrieve data for player " .. player.Name .. ": " .. data)
		playerData[userId] = {Kills = 0, Deaths = 0, Level = 1, Xp = 0}
	end
end

Weapons:

local function loadWeaponData(player)
	local userId = player.UserId
	local PLAYERPACK = wfc(ReplicatedStorage.STARTERPACK, player.Name)
	local success, data = pcall(function()
		return WeaponDataStore:GetAsync(tostring(userId))
	end)

	if success then
		if not data then
			data = {Primary = "M4 Carbine", Secondary = "G17", Special = "Fragmentation Grenade"}
			local success2, error = pcall(function()
				WeaponDataStore:SetAsync(tostring(userId), data)
			end)
			if not success2 then
				warn(error)
			end
		end
		playerWeaponData[userId] = data

		for key, weaponName in pairs(data) do
			local category = AllWeapons[key]
			if category then
				for _,Type in pairs(category:GetChildren()) do
					local weapon = ffc(Type, weaponName)
					if weapon then
						local weaponClone = weapon:Clone()
						weaponClone.Parent = PLAYERPACK
					end
				end
			end
		end
		local Primary = playerWeaponData[userId].Primary
		local Secondary = playerWeaponData[userId].Secondary
		local Special = playerWeaponData[userId].Special
		UpdateWeaponData:FireClient(player,Primary,Secondary,Special)
	else
		warn("CheckWeaponData | Failed to retrieve data for player " .. player.Name .. ": " .. data)
	end
end

The loading and saving is only called when the player joins, and is then saved in a local table.

The structure would be basically like this:
Datastore {
Playerstats{} – Gets stored to local table
Weapons{} – Get stored to another local table
}

1 Like

Something like that ?

local DefaultData = {
	PlayerStats = {
		Kills = 0;
		Deaths = 0;
		Level = 1;
		Exp = 0
	};
	
	Weapons = {
		Primary = "M4 Carbine";
		Secondary = "G17";
		Special = "Fragmentation Grenade"
	}
}

local Success, Data = pcall(function()
	return Datastore:GetAsync(userID)
end)

if Success and not Data then
	Data = DefaultData
end

if Success then
	Player.leaderstats.Kills.Value = Data.PlayerStats.Kills
	Player.leaderstats.Deaths.Value = Data.PlayerStats.Deaths
	
	playerWeaponData[userId] = data

	for key, weaponName in pairs(Data.Weapons) do
		local category = AllWeapons[key]
		if category then
			for _,Type in pairs(category:GetChildren()) do
				local weapon = ffc(Type, weaponName)
				if weapon then
					local weaponClone = weapon:Clone()
					weaponClone.Parent = PLAYERPACK
				end
			end
		end
	end
	local Primary = playerWeaponData[userId].Weapons.Primary
	local Secondary = playerWeaponData[userId].Weapons.Secondary
	local Special = playerWeaponData[userId].Weapons.Special
	UpdateWeaponData:FireClient(player,Primary,Secondary,Special)
	
else
	warn()
end
1 Like

Thanks! You’ve helped me alot with this!

1 Like

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