Datastore not saving player's equipped tool

The code:

local Datastore = game:GetService("DataStoreService")
local mainStore = Datastore:GetDataStore("Main")

local function saveData(player)
	local dataToSave = {
		Inventory = {},
		Stats = {},
		Locker = {}
	}

	
	for _, item in pairs(player.Backpack:GetChildren()) do
		if item:IsA("Tool") then
			table.insert(dataToSave.Inventory, item.Name)
		end
	end
	
	-- Save the characters equipped tool before they leave
	if player.Character and player.Character:FindFirstChildOfClass("Tool") then
		table.insert(dataToSave.Inventory, player.Character:FindFirstChildOfClass("Tool").Name)
	end
	
	print(dataToSave)
end

local function loadData(player)
	
end





game.Players.PlayerAdded:Connect(loadData)
game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		saveData(player)
	end
end)

My issue:
I’m trying to add the player’s equipped tool to Inventory = {} before they leave, but im not sure on how to do that… Any suggestions?

3 Likes

multiple tools or 1 tool at a time?

u want to save equipped tools only right?

Yeah, just the tool thats equipped for the player, I already have the logic for the backpack

at this part:

-- Save the characters equipped tool before they leave
	if player.Character and player.Character:FindFirstChildOfClass("Tool") then
		table.insert(dataToSave.Inventory, player.Character:FindFirstChildOfClass("Tool").Name)
	end

try this:

for _,v in pairs(player.Character:GetChildren()) do
	if v:IsA("Tool") then
		table.insert(dataToSave.Inventory, v.Name)
	end
end
1 Like

Character doesn’t exist on PlayerRemoving you need to cache the tool name in the player as an attribute whenever it gets equipped

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.