Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Storing Dictionary in DataStore?

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

0

Hey could you hop in chat. I am confused about your code.

0

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?

2 answers

What isn't working about the backpack portion? You are always giving the startersword.

Just put the Tool in the starter Team.

2 comments

0

I only want to give the startersword to new players who don't already have weapons saved. I think my problem lies somewhere within loading the saved weapons.

0

Oh let me update, I see.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.