Not if the player can only own/have one building at a time or if the upgrading is a linear path that is predictable. You only need to store a single key: the current building the player owns.
For example, if the buildings are upgraded in a linear path, if the player’s current building is building 3
, then you can assume they owned building 2
as well, and don’t need to store that data.
In datastores, you want to save the least amount of information possible to achieve what you want. You don’t want to save things that can be determined normally in the server scripts from other data.
That would be determined by your game logic and design. Here is a quick example of how something like that could be modelled in a script (this code is not usable by itself, of course):
local DataStoreService = game:GetService("DataStoreService")
--table array storing all buildings available in your game
--contains information about them, could have whatever you want
--the buildings should be orderd from worst to best
local GameBuildings = {
{
Name = "Small Building",
Price = 0,
LevelRequired = 0,
},
{
Name = "Large Building",
Price = 1000,
LevelRequired = 5,
},
{
Name = "Huge Building",
Price = 5000,
LevelRequired = 15,
}
}
local PlayerData = {} --a table of all the data for every player currently in the server
--Example function to load player data. Not for use in a real game.
local function LoadPlayerData(player)
local scope = "Player" .. player.UserId
local playerStats = DataStoreService:GetDataStore("Stats", scope)
local playerUpgrades = DataStoreService:GetDataStore("Upgrades", scope)
--store their data somewhere so it doesn't have to be fetched again
PlayerData[player] = {
CurrentBuilding = playerUpgrades:GetAsync("CurrentBuilding") or 1, --the "or 1" part will give them the 'Small Building' if they have no data
Money = playerStats:GetAsync("Money") or 0,
Level = playerStats:GetAsync("Level") or 1,
--etc
}
end
--Example function to save player data. Not for use in a real game.
local function SavePlayerData(player)
local scope = "Player" .. player.UserId
local playerStats = DataStoreService:GetDataStore("Stats", scope)
local playerUpgrades = DataStoreService:GetDataStore("Upgrades", scope)
local data = PlayerData[player]
if (data) then
--this is for simplicity, don't save data like this normally in a game... use DataStore2 module
playerUpgrades:SetAsync("CurrentBuilding", data.CurrentBuilding)
playerStats:SetAsync("Money", data.Money)
playerStats:SetAsync("Level", data.Level)
PlayerData[player] = nil
end
end
--Example function that is is called (somehow) to upgrade a player's building
local function UpgradePlayerBuilding(player)
local data = PlayerData[player]
if (data) then
local nextBuildingId = data.CurrentBuilding + 1 --next building in the table 'GameBuildings'
local nextBuilding = GameBuildings[nextBuildingId]
if (nextBuilding and data.Money >= nextBuilding.Price and data.Level >= nextBuilding.LevelRequirement) then
--there is a building to upgrade to, and they have enough money and level to upgrade
data.Money -= nextBuilding.Price --charge them for the price
data.CurrentBuilding = nextBuildingId --set the player's current building to the new one they upgraded to
SavePlayerData(player)
end
end
end
game:GetService("Players").PlayerAdded:Connect(function(player)
LoadPlayerData(player)
end)
game:GetService("Players").PlayerRemoving:Connect(function(player)
SavePlayerData(player)
end)