Hey everyone, I’m brand new to DataStores and would like a little extra guidance from those who are familiar.
I am trying to save, and then when the player rejoins, load a number of folders that look like this.
I was able to get everything to save on my own, but couldn’t get it to load, so I scrapped it in hopes of trying a different strategy, but I’m just stumped this time.
I looked around on the forums and found people trying to accomplish something similar, but was never able to find anyone in the exact same boat. Any ideas?
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerPages = DataStoreService:GetDataStore("A")
local ShopData = require(game.ReplicatedStorage.GameMaterials.Modules.ShopData)
function PlayerAdded(p)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local playerSkills = Instance.new("Folder")
playerSkills.Name = "playerskills"
local shopItems = Instance.new("Folder")
shopItems.Name = "shopItems"
local equippedItems = Instance.new("Folder")
equippedItems.Name = "equippeditems"
local pages = Instance.new("IntValue")
pages.Name = "Pages"
pages.Parent = leaderstats
local passive1 = Instance.new("StringValue")
passive1.Name = "Passive1"
passive1.Parent = playerSkills
local passive2 = Instance.new("StringValue")
passive2.Name = "Passive2"
passive2.Parent = playerSkills
local regenRate = Instance.new("NumberValue")
regenRate.Name = "regenRate"
regenRate.Value = 0.3
regenRate.Parent = playerSkills
local storedID = Instance.new("StringValue")
storedID.Name = "storedid"
storedID.Parent = playerSkills
local storedCost = Instance.new("NumberValue")
storedCost.Name = "storedcost"
storedCost.Parent = playerSkills
for i, v in pairs(ShopData) do
print(v.ItemID)
local owned = Instance.new("BoolValue")
owned.Name = v.ItemID
owned.Parent = shopItems
end
for i, v in pairs(ShopData) do
local equipped = Instance.new("BoolValue")
equipped.Name = v.ItemID
equipped.Parent = equippedItems
end
equippedItems.Parent = p
shopItems.Parent = p
leaderstats.Parent = p
playerSkills.Parent = p
-- Separator from just creating the folders and variables under each player
for i = 5, 0, -1 do
local success, data = pcall(function()
return PlayerPages:GetAsync(p.UserId)
end)
if success then
else
print("Something went wrong with loading the data.")
if i == 0 then
p:Kick("Something went wrong loading your data. If problem persists, try again later.")
end
end
end
end
function playerRemoved(p)
local success, newPages = pcall(function()
return PlayerPages:SetAsync(p.UserId)
end)
if success then
print("Saved " .. p.Name .. "'s data successfully!")
end
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(playerRemoved)
I stripped it of the code that was trying to save or load anything to prevent confusion.