I’m trying to make weapons save using DataStore2, but I don’t even know how to make storage into a table. Phantom Forces has a really good DataStore system, where it saves your cases, weapons, skins, etc, and that is basically what I’m trying to accomplish. Sorry for no code provided, I deleted it.
Current Code :
local NewData1
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = 'leaderstats'
leaderstats.Parent = Player
local Points = Instance.new("IntValue")
Points.Name = 'Points'
Points.Parent = leaderstats
local DataStore2 = require(1936396537)
NewData1 = DataStore2("PointsStorage",Player)
print('Worked')
local function UpdatedPoints()
wait()
Points.Value = NewData1:Get()
end
UpdatedPoints()
wait(1)
NewData1:OnUpdate(UpdatedPoints)
UpdatedPoints()
end)
game.Players.PlayerRemoving:Connect(function(Player)
NewData1:Save()
end)
game.ReplicatedStorage.RemoteEvents.CollectWater.OnServerEvent:Connect(function(Player,Water)
Water.Transparency = 0.4
end)
game.ReplicatedStorage.RemoteEvents.DrinkWater.OnServerEvent:Connect(function(Player,Water)
Water.Transparency = 1
Water.Parent.Handle.DrinkSound:Play()
end)
game.ReplicatedStorage.RemoteEvents.GivePoints.OnServerEvent:Connect(function(Player,Amount)
Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + Amount
NewData1:Increment(Amount,0)
Player.leaderstats.Points.Value = NewData1:Get()
end)
game.ReplicatedStorage.RemoteEvents.Purchase.OnServerEvent:Connect(function(Player,Tool,Price)
Player.leaderstats.Points.Value = Player.leaderstats.Points.Value - Price
Tool:Clone().Parent = Player.Backpack
end)
You could have a table for the player’s inventory/tool collection and inside of that table, save a table for each tool owned with the contents of the tool’s table being the amount of said tool they owned and any other data you may need for your tool i.e skins owned etc.
Here’s an example:
local PlayerData = {}
local PlayerDataSchema = {
Inventory = {}
}
--[[
Example tool syntax:
Inventory = {
Sword = {
Amount = 1
}
}
]]
local function GiveTool(player, tool)
tool:Clone().parent = player.Backpack
local ToolData = PlayerData[player.Name].Inventory[tool.Name]
if ToolData then
ToolData.Amount = ToolData.Amount + 1
else
PlayerData[player.Name].Inventory[tool.Name] = {["Amount"] = 1}
end
end
game:GetService("Players").PlayerAdded:Connect(function(Player)
-- get player data here and insert it into table i.e PlayerData[Player.Name] = DataStore2:GetPlayerData(Player.UserId)
end)
RemoteEvent.OnServerEvent:Connect(function(player, request) -- Example remote callback to give a player a sword
if request == "Buy Sword" and player.leaderstats.Coins >= 20 then
player.leaderstats.Coins = player.leaderstats.Coins - 20
GiveTool(player, game:GetService("ReplicatedStorage").Tools.Sword)
end
end)
I’d include DataStore2 usage in my example but I’m not too familiar with the library since I use my own datastore system but I hope this helps you understand going about saving tool data.
Just so you know, your DataStore2 is outdated. Use this one on github:
And I should probably mention that there’s a lot of exploits in your code.
Anyway, let’s just get into the code:
local Items = {
Sword = {
Name = "Sword",
Path = game.ServerStorage.Sword,
Price = 20
}
}
local DataStore2 = require(game.ServerStorage.DataStore2) -- replace with the actual path
local function updateInventory(folder, items)
for name, _ in pairs(items) do
local newItem = Instance.new("ObjectValue") -- objectvalue because it's value can be nil, we're not going to be setting it
newItem.Name = name
newItem.Parent = folder
end
end
DataStore2.Combine("DATA", "points", "inventory") -- this is important, check this: https://kampfkarren.github.io/Roblox/guide/gotchas/
local function playerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local pointsStore = DataStore2("points", player)
local points = Instance.new("IntValue")
points.Name = 'Points'
points.Value = pointsStore:Get(0) -- 0 = default value, not the actual amount
points.Parent = leaderstats
local inventory = Instance.new("Folder") -- we're gonna store a bunch of values with the names of the items in it
inventory.Name = "Inventory"
inventory.Parent = player
update
pointsStore:OnUpdate(function() -- use this because we're going to update the datastore by doing pointsStore:Set() and pointsStore:Increment()
Points.Value = pointsStore:Get(0) -- default value
end
local items = inventoryStore:Get({
Sword = true
})
updateInventory(inventory, items)
inventoryStore:OnUpdate(function()
local items = inventoryStore:Get({
Sword = true
})
updateInventory(inventory, items)
end)
end)
for _, player in pairs(game.Players:GetPlayers()) do -- good practice, especially if there's a delay
playerAdded(player)
end
game.Players.PlayerAdded:Connect(playerAdded)
game.ReplicatedStorage.RemoteEvents.Purchase.OnServerEvent:Connect(function(player, tool) -- just a reminder, don't add a price argument exploiters can easily change it to their liking, but ill keep it for time's sake
local pointsStore = DataStore2("points", player)
local Item = Items[tool]
local price = Item.Price
local inventoryStore = DataStore2("inventory", player)
if pointsStore:Get(0) >= price then
pointsStore:Increment(-price)
local inventory = inventoryStore:Get({
Sword = true
})
inventory[Item.Name] = true
end
end)