I need Advice for my game

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.

is there something wrong with these?
This script is the datasaved table:

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, _G[plr.Name.."inventory"])
		end
	end)
end)

And this is the test script:


local inv = _G[smowish012.."inventory"]

inv.Knife1.Amount = 0

The problem that I see is the fact that you have the Amount in the tables as strings.

the 1 should be a number and not a string.

i get this error for script 2, also i changed the script so that there numbers and not strings:
screenshot
-btw it says the error is in the

part

for the test script add a wait() before you get the _G variable.

You should probably consider migrating to a table or class, as this system seems quite limited.

idk what that is,could u explain?

i did but i still got the same thing

I would create some sort of module you can access from both the client and the server that allows you to quickly and easily access inventory contents, e.g.

local InventoryManager = require(ReplicatedStorage.Shared.InventoryManager)
-- on the server
local playerInventory = InventoryManager:GetPlayerInventory(player)
if not playerInventory[1] then
    InventoryManager:SetPlayerInventory(player, { InventoryManager.newTool("Knife") })
end
-- on the client
local playerInventory = InventoryManager:GetLocalPlayerInventory()
local slotOne = playerInventory[1]

Oh also, you need to have it as 1 string because “smowish012” would be nil.

wait()

local inv = _G["smowish012inventory"]

inv.Knife1.Amount = 0

@Kaid3n22 Hey the script wont save the data,

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"] = 10;
		};
		["Knife2"] = {
			["Amount"] = 10;
		};
		["Knife3"] = {
			["Amount"] = 10;
		};
		["Knife4"] = {
			["Amount"] = 10;
		};
		["Knife5"] = {
			["Amount"] = 10;
		};
	};
	game.Players.PlayerRemoving:Connect(function(player)
		if plr == player then
			invds:SetAsync(plr.UserId, _G[plr.Name.."inventory"])
		end
	end)
end)

i have a script that will add +1 to the Knife1.Amount, but everytime i leave it goes back to 10

thats because the inventory variable is where it is gotten, so it should be like this:

game.Players.PlayerAdded:Connect(function(plr)
	_G[plr.Name.."inventory"] = invds:GetAsync(plr.UserId) or {
		["Knife1"] = {
			["Amount"] = 10;
		};
		["Knife2"] = {
			["Amount"] = 10;
		};
		["Knife3"] = {
			["Amount"] = 10;
		};
		["Knife4"] = {
			["Amount"] = 10;
		};
		["Knife5"] = {
			["Amount"] = 10;
		};
	};
	
	--btw I'm moving the saving part
end)

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

--because studio shuts the game down and doesn't fire the PlayerRemoving function

game:BindToClose(function()
	for i,plr in pairs(game.Players:GetPlayers() do
		invds:SetAsync(plr.UserId, _G[plr.Name.."inventory"])
	end
end)

it didn’t save on studio because of it not firing the PlayerRemoving event so I added BindToClose. Also, another reason it didn’t save was because you never got the data for the _G variable and left it as inventory variable meaning it never got the data. It should work now.

btw @Kaid3n22 would you know what the key and scope and name of the datastore is?
-im trying to use datastore editor 3

the name of the datastore is Inventory and the scope is the id of the game I’m pretty sure. I don’t use datastore editor much.
edit: scope is something you don’t need to enter. you only need the name of the data store. Also, the key is the id of the player.