I need Advice for my game

Hello there! I kind of need some help with my game, so currently I have a folder inside each player called WeaponsFolder and basically in this folder there are Number Values of Weapons example of what I’m talking about:
screenshot
so basically the reason I have these values is because I have a system where if you buy a knife lets say you buy “Knife1” it’ll take away x amount of money and add +1 to the value of that knife…
but is there another way I could do stuff like this, like without having a cluttered folder? also btw if you have any ideas please consider thinking if they can be datasaved in a datastore…
-Thanks for listening and feel free to comment any questions :slight_smile:

–Here’s a revised review of what im talking about

Still don’t quite understand. Can you give us another example?

No problem! Basically I have a folder in each player called “WeaponsFolder” and this folder has a bunch of number values with the label of each knife the player owns, also the value in knife value is the amount they own. Lets say if you own 6 Knife1’s then the value of Player.WeaponsFolder.Knife1.Value = 6
and that’s how I’ve been making a inventory system. But I want to see if I should keep doing it this way or if I should do another way to make an inventory system.

you could keep the inventory in the table like this or keep the number values, it doesn’t change much either way

local inventory = {
["Knife1"] = 6,
["Knife2"] = 2,
}

yeah, I’ve been thinking about using tables but the problem with it is that I don’t know how to 1, datasave them, and 2, I don’t know how to have a table designed for a specific player, could you help me with those questions?

You use the :GetAsync() and :SetAsync() functions of DataStores to save data and get it. Sort of like this:

local DS = game:GetService("DataStoreService")
local invds = DS:GetDataStore("Inventory")

game.Players.PlayerAdded:Connect(function(plr)
local inventory = invds:GetAsync(plr.UserId) or {}
--do whatever with the data
--to save, because it's easier this way for data that isn't an instance
game.Players.PlayerRemoving:Connect(function(player)
if plr == player then
invds:SetAsync(plr.UserId, inventory)
end
end)
end)

alrigthy ill try that, but if it does work how work what would the path be?
If your confused on what im talking about then ill explain:
Lets say i wanted to make a script that would check if the player owned “Knife1” how would i be able to check it?

something like this should work
script1:

local DS = game:GetService("DataStoreService")
local invds = DS:GetDataStore("Inventory")

game.Players.PlayerAdded:Connect(function(plr)
_G[plr.Name.."inventory"] = invds:GetAsync(plr.UserId) or {}
end)

game.Players.PlayerRemoving:Connect(function(player)
invds:SetAsync(plr.UserId, _G[plr.Name.."inventory")
end)

in another script:

--make sure to get the plr or plr's name for this
local inv = _G[plrName.."inventory"]

if table.find(inv, Knife1) then
print("Knife1 found")
end

_G shares information between scripts as long as you do _G.whatever = "idk" then ask for it in another script.

1 Like

Using _G is deprecated, I do not suggest it.

What is “invds”? it shows as unknown.

whoops sorry, I’ll edit that it was a mistake because I was in a hurry

if i shouldnt do that then what should i do?

_G is not deprecated, but most people don’t see it as a good practice.

Inside of the folder, you could probably create more folders which give the knives different attributes. It would also give more leniency if you want to add more attributes in the future.

Example:

WeaponsFolder
    > Knife1: Folder
        > Value = 1000
        > Colour = Red
        > Rarity = Legendary
    > Knife2: Folder
        > Value = 100
        > Colour = Purple
        > Rarity = Common

etc...

is there a way i could do something like this?

local DS = game:GetService("DataStoreService")
local invds = DS:GetDataStore("Inventory")

game.Players.PlayerAdded:Connect(function(plr)
	local inventory = invds:GetAsync(plr.UserId) or {}

	_G[plr.Name.."inventory"] = inventory

	inventory = _G[plr.Name.."inventory"] --so when you add stuff to inventory, it affects the _G too.
	inventory = {
		["Knife1"] = {
			["Amount"] = "1";
		};
		};
	game.Players.PlayerRemoving:Connect(function(player)
		if plr == player then
			invds:SetAsync(plr.UserId, inventory)
		end
	end)
end)

i want to be able to make something like this when i do make the table:

	["Knife1"] = {
		["Amount"] = "1";
	};
	["Knife2"] = {
		["Amount"] = "1";
	};
	["Knife3"] = {
		["Amount"] = "1";
	};
	["Knife4"] = {
		["Amount"] = "1";
	};
	["Knife5"] = {
		["Amount"] = "1";
	};

};


yes, you can do something like that. _G is basically just a variable that goes across all scripts. Although, you would need to update the _G variable again :sweat:

ok would i update it like this:

local DS = game:GetService("DataStoreService")
local invds = DS:GetDataStore("Inventory")

game.Players.PlayerAdded:Connect(function(plr)
	local inventory = invds:GetAsync(plr.UserId) or {}

	_G[plr.Name.."inventory"] ={
	["Knife1"] = {
		["Amount"] = "1";
	};
		["Knife2"] = {
		["Amount"] = "1";
	};
		["Knife3"] = {
		["Amount"] = "1";
	};
		["Knife4"] = {
		["Amount"] = "1";
	};
		["Knife5"] = {
		["Amount"] = "1";
	};
};
	game.Players.PlayerRemoving:Connect(function(player)
		if plr == player then
			invds:SetAsync(plr.UserId, inventory)
		end
	end)
end)

yes, that would work for updating it.

Also, for this like It should be

invds:SetAsync(plr.UserId, _G[plr.Name.."inventory"])

Whatever, you still should not use it as the variable might not be there every time.
https://devforum.roblox.com/t/global-turtorial-g/1156902#:~:text=_G is generally advised against%2C but it is,isn’t guaranteed to exist when you access it.