I'm having a problem with my datastore, the stats portion is working properly, but the backpack portion isn't. The backpack portion always defaults to giving the player the startersword. I was wondering if there were any obvious issues in the code that I may have overlooked; and also if there is a better way to go about doing this.
local Data = game:GetService("DataStoreService"):GetDataStore("Data")
game.Players.PlayerAdded:Connect(function(Player)
local PlayerData = Data:GetAsync(Player.UserId)
setStats(Player, PlayerData["Stats"])
setBackpack(Player, PlayerData["Backpack"])
end)
function setStats(Player, SavedStats)
if SavedStats then
print("SavedStats")
end
createStats(Player)
local Stats = Player.Stats
if SavedStats and SavedStats[1] > 0 then
Stats.Level.Value = SavedStats[1]
Stats.Exp.Value = SavedStats[2]
Stats.Gold.Value = SavedStats[3]
else
Stats.Level.Value = 1
Stats.Exp.Value = 0
Stats.Gold.Value = 25
end
end
function createStats(Player)
local Stats = Instance.new("Folder", Player)
Stats.Name = "Stats"
local Level = Instance.new("IntValue", Stats)
Level.Name = "Level"
local Exp = Instance.new("IntValue", Stats)
Exp.Name = "Exp"
local Gold = Instance.new("IntValue", Stats)
Gold.Name = "Gold"
end
function getStats(Player)
local StatsTable = {}
if Player:FindFirstChild("Stats") then
for _, Stat in pairs(Player.Stats:GetChildren()) do
table.insert(StatsTable, Stat.Value);
end
end
return StatsTable
end
function setBackpack(Player, SavedWeapons)
if SavedWeapons then
print("SavedWeapons")
end
if SavedWeapons and #SavedWeapons ~= 0 then
for _, WeaponName in pairs(SavedWeapons) do
print("Weapon for loop")
local Weapon = game.ReplicatedStorage.Weapons:FindFirstChild(WeaponName)
if Weapon then
print("Loading " .. Weapon.Name)
Weapon:Clone().Parent = Player:WaitForChild("Backpack")
Weapon:Clone().Parent = Player:WaitForChild("StarterGear")
end
end
else
print("Giving " .. Player.Name .. " StarterSword")
local StarterSword = game.ReplicatedStorage.Weapons:FindFirstChild("StarterSword"):Clone()
StarterSword.Parent = Player:WaitForChild("Backpack")
end
end
function getBackpack(Player)
local WeaponsTable = {}
local EquippedWeapon = getEquippedWeapon(Player)
if EquippedWeapon then
print("Saving " .. EquippedWeapon.Name)
table.insert(WeaponsTable, EquippedWeapon.Name)
end
for _, Weapon in pairs(Player.Backpack:GetChildren()) do
print("Saving " .. Weapon.Name)
table.insert(WeaponsTable, Weapon.Name)
end
return WeaponsTable
end
function getEquippedWeapon(Player)
local Character = game.Workspace:FindFirstChild(Player.Name)
if Character then
for _, Item in pairs(Character:GetChildren()) do
if Item:IsA("Tool") then
print("Equipped tool " .. Item.Name)
return Item
end
end
end
return
end
game.Players.PlayerRemoving:Connect(function(Player)
pcall(function()
local DataDictionary = {}
DataDictionary["Stats"] = getStats(Player)
DataDictionary["Backpack"] = getBackpack(Player)
Data:SetAsync(Player.UserId, DataDictionary)
end)
end)

2 comments
Hey could you hop in chat. I am confused about your code.
Can't really spot any problematic code at the moment other than the PlayerData table being empty or nil if the player has never joined the game. Is this a studio problem or published problem?