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...
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:
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
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]
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.
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.