How do I make an inventory script that stores data when you leave the game?

a custom one, sorry forgot to say it. also, the parts are placed in the workspace because i made the game to be built so the parts fall from the sky

Sry bro I’m not currently home so I can’t make the script.

But u can make a bool value for each item. And when the player is leaving u can check what the player currently has in their inventory and check the bool values for that item. Then u can save the bool values. Then when the player is added u can see what bools r true and add back whatever items r true.

I also forgot to mention to parent the bools to the player

okay, and how do i make the inventory system from scratch

Maybe try this one: Open Sourced Inventory/Backpack system
I haven’t used it myself but u can give it a try. Also u can try searching up on how to make ur own inventory system and there r many YT vids and dev forum posts on it.

i dont think i have elaborated myself enough, so apologies for the lack of information.

i am trying to make a custom inventory system, so when the player clicks on a part, it saves inside the inventory after its destroyed. the parts are being spawned from a module script. and when the player leaves the game, it saves all the collected parts for the player so that when they rejoin, it reloads it back for them.

yeah thats my clarification, hope this gives more context to my question

tl;dr an inventory system like this game:

While I have never made a proper inventory system myself, I can say the solution PIZZA came up with by saving a bool and perhaps an int aswell should make this work easily.

Simply make the guis and stuff, make a bool for every item you have and add an int value to check the amount you have if you plan to allow multiple copies in the inventory.

Bool and Int values can be saved through DataStorage so whenever you load/deload you just add the gui for the items in question back into the game when joining.

Put a clickdetector or something similar in the part you want to collect and have that detector modify the preset values for that item which then automatically adds it into the inventory because of the refresh you have on it (refresh it every 1-5 seconds to accommodate for change).

With the information you have been provided you can start tinkering around to make a system like what you are aiming for, you just have to try it out now.

hope some of that helped and cleared up some stuff.

You need DataStorage to make this system, the server itself cannot save your data for you.

You will need to have Values add the item gui parts automatically upon joining in the scenario that you may own one of the items.

Save the values you have for owning the item and the potential quanitity owned inside tables for datastorage to save, then load that data upon joining the game again.

I encourage you to look into how datastorage works incase you may not already know how it works.

I’m not the one asking for the system, OP is.

You might not be able to save values directly to datastores but you can save an array/table.
Pretty sure I already said that you can’t save instances to datastores, thats why you have to put it into an array or table to save it to Datastorage.

My implied method saves a value, like an ID of the item tied to the bool which checks if you own it or not to datastorage which I have done before and has worked just fine.

im honestly kinda new to scripting, and i dont know how to code the system. are there any posts or yt videos i can follow to achieve my goal? (it would be recommended if the post/video made use of a base part)

Here a video by alvinblox on datastores.

for the rest of this stuff you will need to dig a bit into GUI creation and stuff.

1 Like
local tools = --folder that should be located in ReplicatedStorage called Tools (included image shows how it should look)
local key = game:GetService("DataStoreService"):GetDataStore("name it whatever")

game.Players.PlayerAdded:Connect(function(plr)
	if key:GetAsync(plr.UserId) ~= nil then
		for i,v in key:GetAsync(plr.UserId) do
			if tools:FindFirstChild(v) then
				local tool = tools[v]:Clone()
				tool.Parent = plr.Backpack
			else
				warn(debug.traceback("Tool not found."))
			end
		end
		print("Loaded tools!")
	else
		print("New user - data is nil")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local tool_table = {}
	for i,v in pairs(plr.Backpack:GetChildren()) do
		table.insert(tool_table, v.Name)
	end
	key:SetAsync(plr.UserId, tool_table)
	print("Saved tools!")
end)

Here’s how the folder should look:
Screenshot 2024-11-03 141607
Also make sure DataStore access is enabled! :slightly_smiling_face:

1 Like

in this case i would like the tool to be changed into a part, would this code be viable if i changed the tools into a part?

You want to store a part?

Character bypass
1 Like

what does it do? can you explain?

It takes all the tools from your backpack and stores them in a datastore. When you join back the tools get cloned from the Tools folder and put inside of your backpack again.

does this DataStore work with large amounts of players at once, or is it one script per player?

Ohh, I understand what you’re trying to do now.
You want to store a part instead of a tool.

Anyways, you can use that script for storing parts aswell, here’s how:

  • Create a Tool.
  • Create a Part inside the Tool named Handle.
  • Make sure to not Anchor it.
    This will create a tool you can equip which will be a part.