Help fixing my tools saving DataStore

So I’m trying to create a DataStore to save my Tools Names so when I rejoin I get them, but saving them into a Table. I did it another way and it worked but that way only Saved 1 Tool that’s why I’m trying to redo it. Here is my script:

local DSS = game:GetService("DataStoreService")
local myDataStore = DSS:GetDataStore("TDS") -- Tools Data Store
local RS = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Char)
	local ToolsTable = {}
	local TableData
	local SwordData
	local GunData
	local Backpack = Player.Backpack
	local WeaponsFolder = RS.Weapons
	local Success, ErrorMsg = pcall(function()
		TableData = myDataStore:GetAsync(Player.UserId.."TOOLSKEY")-- Our datastore keys	
		SwordData = myDataStore:GetAsync(Player.UserId.."GunKey")
		SwordData = myDataStore:GetAsync(Player.UserId.."SwordKey") 
	end)
	if Success then
		print("Data successfully gotten")
		for i,v in pairs(ToolsTable) do
			if WeaponsFolder:FindFirstChild(v) then
				WeaponsFolder[v]:Clone().Parent = Player.Backpack
			end
		end
	else
		print("Error while getting data")
		warn(ErrorMsg)
	end

game.Players.PlayerRemoving:Connect(function(Player)
	local Success, ErrorMsg = pcall(function()
	myDataStore:SetAsync(Player.UserId.."TOOLSKEY", ToolsTable)
	myDataStore:SetAsync(Player.UserId.."GunKey", table.find(ToolsTable, WeaponsFolder.Pistol.Name))
	myDataStore:SetAsync(Player.UserId.."SwordKey", table.find(ToolsTable, WeaponsFolder.ClassicSword.Name))

	end)
if Success then
	print("Data saved")
		else
	print("Error saving data")
	warn(ErrorMsg)
end
end)

Backpack.ChildAdded:Connect(function(Child)
	print("added child")
	for i,T in pairs(WeaponsFolder:GetChildren()) do
		if Child.Name == T.Name then
			print("new tool")
			table.insert(ToolsTable, Child.Name)
		end
	end	
end)
end)
end)

Everything prints besides the “if Success then” and "else print(“Error while saving data”) those don’t print when I leave, only in studio though. Idk if it prints in the actually game but I know everything else does.

Me getting the tool

Me after rejoining

You’re iterating through the newly constructed ToolsTable when TableData is the data saved of your Tools.

Additionally, I recommend using a Dictionary to store Player ToolsTables in rather than adding new Connections on CharacterAdded which are never Disconnected. Oh and you can use FindFirstChild to find a Child in an Instance or if it even exists (it’ll return nil if it doesn’t).

For example:

--// Start of the Script
local ToolTables = {};

--// Inside PlayerAdded, Backpack ChildAdded connection
local FoundTool = WeaponsFolder:FindFirstChild(Child.Name);
if (ToolTables[Player] and FoundTool) then
    table.insert(ToolTables[Player], FoundTool);
end

--// Inside CharacterAdded
ToolTables[Player] = {};

Okay thank you so much! 30 chars

1 Like

Actually, I think I figured out another problem. I was supposed to use

if Data then

Instead I used if Success then for the giving tools.

1 Like