Hello every one! I hope every one is having an amazing summer! I am working on a game with an inventory system, wich I’m now trying to save in a data store. I came up with a script to save data from any kind of tool you picked up, but somehow I have a problem.
There are no errors but every time I run the script (below), it first prints the values in the data table wich are saved, but when the values need to be set to the inventory it just sets to 0. I already tried a couple of things but nothing seems to do the job. Can someone help me? Here is the script and output:
local DataStoreService = game:GetService(‘DataStoreService’)
local myDataStore = DataStoreService:GetDataStore(‘DataStore’)
game.Players.PlayerAdded:Connect(function(player)
local inventory = Instance.new(‘Folder’, player)
inventory.Name = ‘Inventory’
for i, v in pairs(game.ReplicatedStorage.inventoryItems:GetChildren()) do
local var = Instance.new('IntValue', player.Inventory)
var.Name = v.Name
end
local playerUserId = "Player_"..player.UserId
local data
local success, errorMessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
print(table.unpack(data))
end)
print(table.unpack(data))
if success then
print(table.unpack(data))
for i, v in pairs(player.Inventory:GetChildren()) do
v.Value = tonumber(data[v.Name])
wait()
print(v.Value)
if v.Value == 0 then
print('EMPTY')
else
local item = game.ReplicatedStorage.inventoryItems:FindFirstChild(v.Name)
local stick = item:Clone()
local place = player.PlayerGui:WaitForChild('InventoryGui')
stick.Parent = place.Frame
wait()
stick.Count.LocalScript.Enabled = true
print('Copied')
end
print(table.unpack(data))
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId
local data = {}
for i, v in pairs(player.Inventory:GetChildren()) do
local var = v.Name
var = v.Value
table.insert(data, var)
end
local success, errorMessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print('Saved Successfully!')
else
print('Saving Error!')
warn(errorMessage)
end
end)
OUTPUT:
- when player joins the game:
-when player leaves the game:
hope some one can help me. Thanks in advance!