How could i make a sort of purchase saving system?

Hello, I’m Julien999111
I was wondering how to make a purchase system that saves purchases with a table, like when you purchase something, it adds it to the table and takes away, say, 10 coins.

I’m looking to make this in a gui, so it fires and then does all the stuff above.

Here are some more details:
image
if “part” is not in the table which my inventory-type-thing saves, then itll say purchase,
or if “part” is already in my table (which i dont know how to check tables for objects) itll say “equip”… like this:
image
in summary, im looking for a way to save purchases in-game, like roblox saves gamepasses.
i know what datastores are and all, im unfamiliar with tables and how to even usedatastores

2 Likes

Before you get flagged by somebody, move this post to #help-and-feedback:scripting-support

You would utilize Roblox’s DataStore Service. This is the only way to save data between sessions (besides modules that use Datastore such as ProfileService)

could you be more specific? i know what datastore is, but i dont know how to save tables, which is what im aiming for

like, i dont know what to do on the second line here
image
i know the second line makes no sense, its just the general idea
but i want to make it so it adds the item name to the player’s table, which of course needs to be saved

1 Like

If you want to save something, use DataStores. I’ve made a few replies in the past regarding DataStore2, here is a copy;

--Script in ServerScriptService:

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")

-- Wait for our DataStoreVersion object to spawn in, and get it's value
local dV = ServerStorage:WaitForChild("DataStoreVersion").Value
-- Wait for our DataStore2 module object to spawn in, and require it
local dataStore2 = require(ServerStorage:WaitForChild("DataStore2"))

-- Combine all our DataStores into one
dataStore2.Combine(
	"MasterKey"..dV, -- Dont change this one
	"Slaps"..dV -- Your slaps.
	--"Slaps2"..dV -- Just a example of more data you want to save, could also be named coins ect. (Just remember to add a comma after the dV in the Slaps above
)
-- Define DefaultData for our DataStore's
local Default_Slaps = 0
--local Default_Slaps2 = 0


Players.PlayerAdded:Connect(function(Player)
	-- Make a warn in output, to tell us that the Player's data started loading
	warn("[SERVER]: Started loading [PLAYER: "..Player.Name.."'s] Datastore-data!")
	Player:SetAttribute("DataLoadingFinished", false) -- Can be used by other scripts, to determine if our data is loaded, before we do change it their data
	-- If you want, just change the Folder below to be your leaderstats folder
	-- Create a Folder for our replicated PlayerData, and put it inside our Player
	local Folder_PlayerDataFolder = Instance.new("Folder");Folder_PlayerDataFolder.Name = "PlayerData";Folder_PlayerDataFolder.Parent = Player
	-- Create a IntValue to store the number of Slaps
	local Value_Slaps = Instance.new("IntValue");Value_Slaps.Name = "Slaps";Value_Slaps.Parent = Folder_PlayerDataFolder -- Used to replicate our current data to the player or any player in the server
	-- Create a IntValue to store the number of Slaps
	--local Value_Slaps2 = Instance.new("IntValue");Value_Slaps2.Name = "Slaps2";Value_Slaps2.Parent = Folder_PlayerDataFolder

	local DS_Slaps = dataStore2("Slaps"..dV,Player) -- Define our Slaps Datastore
	--local DS_Slaps2 = dataStore2("Slaps2"..dV,Player)

	local function Update_Slaps(Value) -- We create a function here, to always update the replicated data to be equal value to our datastore data. This function will run everytime the value of Slaps in our datastore updates
		Value_Slaps.Value = Value
		warn("[SERVER]: Update_Slaps ran...")
	end

	--[[local function Update_Slaps2(Value)
		Value_Slaps2.Value = Value
		warn("[SERVER]: Update_Slaps2 ran...")
	end]]

	Update_Slaps(DS_Slaps:Get(Default_Slaps)) -- We run the Update_Slaps function, to initialize the default replicated Slaps value in our PlayerData folder. ":Get()" gets our current data of Slaps. "Default_Slaps" is used if no data was found, hit to why it's the default value.
	--Update_Slaps2(DS_Slaps2:Get(Default_Slaps2))

	DS_Slaps:OnUpdate(Update_Slaps) -- Everytime our Slaps data in our DataStore updates, run the Update_Slaps function
	--DS_Slaps2:OnUpdate(Update_Slaps2)

	-- OnCharacterAdded
	Player.CharacterAdded:Connect(function(Character) -- Since the character does not automatically spawn, we create a function to make it automatically spawn on death, after we've finished loading all the data. It's essentially the same thing the CharacterAutoLoads does.
		local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			task.wait(5) -- Delay before spawn, change to your liking.
			Player:LoadCharacter()
		end)
	end)

	warn("[SERVER]: Finished loading [PLAYER: "..Player.Name.."'s] Datastore-data!")
	Player:SetAttribute("DataLoadingFinished", true)
	Player:LoadCharacter() -- Also turn off CharacterAUtoLoads under "Players" in Explorer. Normally we wont have the player to spawn before all the data is loaded
end)

If you want to know how to save tables, just ask.

2 Likes

alright, ive figured something else out, i just need to know how to add to tables, check if something is in a table, and save them

Hi!
So something the only difference is how you call & update a table/dictionary.

-- Lets say We have 3 Coins & 2 Apples in our currency
-- Our test-currency looks like the following:
local Sample = {
	["Apples"] = 2,
	["Coins"] = 3
}

local DS_Currency = dataStore2("Currency"..dV,Player)
local DICT_Currency = DS_Currency:Get() -- We get the current Table/Dictionary with Currencies
DICT_Currency["Coins"] += 1 --We add 1 to the Coins value
DS_Currency:Set(DICT_Currency) -- We insert the whole updated Table/Dictionary
-- We now have 4 Coins & 2 Apples in our currency
1 Like

so, how would u save multiple strings, like if a player buys a chair or a book, it adds “chair” and “book” to the table

and when it saves the table, its not saving multiple values, its saving all the items in the table for one value, say its name is “Items” and its a string value, that holds the table where the player’s items are stored

--template table for data
local DataTemplate = {
   OwnedItems = {}
end

local SessionData = {}

--when a player joins
SessionData[player] = DataTemplate --only if they joined for first time, otherwise use GetAsync() for previous data

--adding stuff to player data
table.insert(SessionData[player].OwnedItems, "Book")

--Saving the table
SomeDataStore:SetAsync(playerKey, SessionData[player])

Correct, but that is intented.
If you want to add something new to Currency, just do;

-- Lets say We have 3 Coins & 2 Apples in our currency
-- Our test-currency looks like the following:
local Sample = {
	["Apples"] = 2,
	["Coins"] = 3
}

local DS_Currency = dataStore2("Currency"..dV,Player)
local DICT_Currency = DS_Currency:Get() -- We get the current Table/Dictionary with Currencies
DICT_Currency["Chair"] += 1 --If Chair is not present, we add the key "Chair" & adds 1 to the Chair value
DS_Currency:Set(DICT_Currency) -- We insert the whole updated Table/Dictionary
-- We now have 3 Coins, 2 Apples & 1 Chair in our currency
-- To remove something from currency, you can either set the value to 0, then it's still in the table/dictionary, or set the value to nil, then it's removed fully from the table/dictionary
DICT_Currency["Chair"] = 0 -- still in table/dictionary, but value is 0
DICT_Currency["Chair"] = nil -- removed from table/dictionary
1 Like

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