I need Advice for my game

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.