I'm basically trying to convert my DataStore1 Script Into a DataStore two. Only problem being is that I'm not sure how to check if the player has Data using an If (Like this if Datastore:GetAsync(player.UserId) then) and then looping that data all in DataStore two( Looping like this for i,v in pairs(Datastore:GetAsync(player.UserId)) do
I'll post both my DataStore 1 and my attempt at a DataStore two to give you guys even more of an understanding. Thanks
DataStore 1
local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerDataV1");
local itemlist = {}
game.Players.PlayerAdded:Connect(function(player)
local inv = Instance.new("Folder",player)
inv.Name = "Inventory"
inv.ChildAdded:Connect(function(part)
print(part.Name.." Was Added to "..player.Name.."'s Inventory")
end)
if Datastore:GetAsync(player.UserId) then -- This Line Check if the Player Has Data(Need to Convert)
for i,v in pairs(Datastore:GetAsync(player.UserId)) do -- This Line Loops Through The Data(Need To Convert)
print("Name: " .. v[1] .. " || Price: " .. v[2])
local Item = Instance.new("IntValue", inv)
Item.Name = v[1]
Item.Value = v[2]
end
else
print("Player Has No Data")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for i,v in pairs(player.Inventory:GetChildren()) do
table.insert(itemlist, {v.Name, v.Value})
print("No Loop?")
end
Datastore:SetAsync(player.UserId, itemlist)
print("saved")
end)
DataStore 2
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local Workspace = game:GetService("Workspace")
local DataStore2 = require(ServerScriptService.DataStore2)
DataStore2.Combine("InventoryDataV1", "Items")
local itemlist = {}
game.Players.PlayerAdded:Connect(function(player)
local inv = Instance.new("Folder",player)
inv.Name = "Inventory"
local IventoryStore = DataStore2("Items", player)
inv.ChildAdded:Connect(function(part)
print(part.Name.." Was Added to "..player.Name.."'s Inventory")
end)
if IventoryStore:GetAsync(player.UserId) then
print("Working")
for i,v in pairs(IventoryStore:GetAsync(player.UserId)) do
print("Name: " .. v[1] .. " || Price: " .. v[2])
local Item = Instance.new("IntValue", inv)
Item.Name = v[1]
Item.Value = v[2]
end
else
print("Player Has no Data")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local IventoryStore = DataStore2("Items", player)
for i,v in pairs(player.Inventory:GetChildren()) do
table.insert(itemlist, {v.Name, v.Value})
print("No loop?")
end
IventoryStore:SetAsync(player.UserId, itemlist)
print("saved")
end)
prints are used to see if things were working
Regards, Bl_ueHistory
