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