How do you save buildings?

Hello,

As you can see in the title of this topic, I want to know how I can save buildings.
So I already know how I can save currency, but now I want to know how I can save buildings.

I mean with saving buildings this;

for example: A player can buy 2 buildings, a small building and a large building but…
When they just started to play the game they automatically start with a small building but later when they got enough cash to buy a bigger building, then the small building is going to be replaced by the bigger building. But when the player leaves and then later he will join the game again he doesnt starts with the small building ofcourse but with the bigger building.

Can someone explain how you do this?

You don’t need to write entire scripts!

Wouldn’t this be done using datastores? I would review datastore’s Wiki page again.

He is talking about player’s in-game. Not actually saving a model.

I meant saving it with data store.

So, should I be able to put the models name in the script with the data store?

All you would do is save the name or ID of the building they own into datastore, and then you would load the correct building depending on that value similarily to currency.

I understand that but does the bigger building automatically replaces the smaller building? Because that does happen with currency.

If it is saved in the same datastore key then yes, it will replace the smaller building as it will overwrite the key. To not achieve that you would instead store some sort of table like how you would save a player’s inventory items.

So every single building needs to be in the datastore so it can replace the better one? But how does the script knows that building 2 is better than building 1? Thats my last question.

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)

This would really help me! Thanks for you’re help :upside_down_face:

1 Like