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)
What isn't working about the backpack portion? You are always giving the startersword.
I just thought about it, but does the player have any other weapons on them? If not then there is no problem since startersword would be saved to the backpack like any other weapon would. All you have to do is check if the weapon isn't named startersword before saving it to the table.