Using a DataStore to store ownership status on Vehicles

Aye, so I’m starting to experiment with DataStores and I’m currently developing a system where you’d have a Dealership to Buy or Sell Cars and a Garage to view and spawn your owned cars…

Would it be viable to have a “IsOwned” Value and so once you buy it it’s set to true so your vehicle shows up at the Garage and it saves to the datastore so it saves that blah blah.CarBrand.Car.IsOwned = true

Is there any simpler or easier way to do this (or is it even done that way)

local dss = game:GetService("DataStoreService")
local saveAllPlayerData = dss:GetDataStore("SaveAllPlayerData")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	
	local items = Instance.new("Folder",player)
	items.Name = "items"
	leaderstats.Name = "leaderstats"
	
	local Clicks = Instance.new("IntValue", leaderstats)
	Clicks.Name = "Clicks"
	Clicks.Value = 0
	
	local Gold = Instance.new("IntValue", leaderstats)
	Gold.Value = 0
	Gold.Name = "Gold"
	
	local Diamond = Instance.new("IntValue", leaderstats)
	Diamond.Value = 0
	Diamond.Name = "Diamond"
	
	local SpeedCoil = Instance.new("BoolValue",items)
	SpeedCoil.Value = false
	SpeedCoil.Name = "Speedcoil"

	local GravityCoil = Instance.new("BoolValue",items)
	GravityCoil.Value = false
	GravityCoil.Name = "Gravitycoil"
	
	local data = saveAllPlayerData:GetAsync(player.UserId)
	
	if data ~= nil then
		Gold.Value = data[1]
		Diamond.Value = data[2]
		Clicks.Value = data[3]
		SpeedCoil.Value = data[4]
		GravityCoil.Value = data[5]
	else
		Gold.Value = 0
		Diamond.Value = 0
		Clicks.Value = 0
		SpeedCoil.Value = false
		GravityCoil.Value = false
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local savedData = {}
	
	table.insert(savedData, player.leaderstats.Gold.Value)
	table.insert(savedData, player.leaderstats.Diamond.Value)
	table.insert(savedData, player.leaderstats.Clicks.Value)
	table.insert(savedData, player.items.Speedcoil.Value)
	table.insert(savedData, player.items.Gravitycoil.Value)
	
	
	saveAllPlayerData:SetAsync(player.UserId, savedData)
end)

This saves a table, this is pretty much a very nice way of doing it, try it. (yeah, you’ll have to add each car manually, but it’s worth it when it works!)
Edit: This is what i use for my game.

You should always wrap SetAsync & GetAsync in a pcall function, sometimes they can error due to the fact it needs to connect to Roblox’s web servers to actually save the data.

I can see you check if the data is nil, and if it is you reset the players data? This isn’t great because if a player has progress and roblox fails to load their data it completely wipes it; if the save succeeds when the player leaves, you should instead kick the player to prevent data loss.

In my opinion, the code you have provided isn’t very good practise and shouldn’t be used.


@ivx_n

I would recommend using a Datastore wrapper such as ProfileService or DS2; there are hundreds of tutorials on how to use these wrappers. If you don’t want to use these then you could just research about basic DSS which may be more confusing for a beginner.

There’s not really much use in wrapping DataStore method calls inside a call to pcall() unless you intend to do something if the code inside the pcall() fails (errors), otherwise you may as well just be making vanilla calls to the DataStore service methods, simply printing/warning when a failure occurs won’t fix anything.

You should create a folder of BoolValue instances (parented to the player’s instance) which correspond to the ownership status of each individual car, false if unowned and conversely true if owned, then when a vehicle is purchase switch its corresponding BoolValue instance’s value from false to true indicating that the vehicle has been purchased, at any point in time when a vehicle purchase is made or the player leaves the game or the server shuts down you should iterate over the folder of BoolValue instances inside the player and then store the data of each BoolValue (name, class, value) inside a table value which can then be stored inside the DataStore, then when the player next joins you should create those same BoolValue instances inside a folder parented to the player and then retrieve their data from the DataStore and set the values of those BoolValue instances accordingly.

1 Like