How to save an equipped tool upon leaving

im trying to save a player’s equipped tool but i can’t even detect if a player has an equipped tool when they leave in the first place

code:

Players.PlayerRemoving:Connect(function(Player)
	
	local Tool = Player.Character:FindFirstChildOfClass("Tool")
	
	if Tool then
		print("Tool found inside character.")
	end
end)

it doesnt print anything, but it does error “attempt to index nil with ‘FindFirstChildOfClass’”

When the player equips the tool, store it in a table on the server and save that when the player leaves. The tool gets deleted with the player before you can save it.

The way I check whether or not player is holding a tool on the server is like this:

local function checkTools(player)
	for i,v in pairs(player.Character:GetChildren()) do
		if v:IsA("Tool") then
			return v
		end
	end
end

You could set up a datastore function to store the output of this function when a player leaves the server.

Maybe the issue is that by the time the player exit event fires, the character has already been destroyed. Perhaps you could try this:

  • Player’s character fires the removing event:
  • Add this to a table / dictionary , The key is the player, the value is a table consisting of the tool and a character added connection
  • if the character added connection fires and the character respawns, clear the player’s index on the table
  • if the player leaves, you have the tool and you can just clear the table on the players index

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