Server's not being able to get the player's character

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want my script to save the tool that are in a leaving player’s backpack

  2. What is the issue? since a tool goes to a player’s character while being equipped I’ve tried to loop through the player’s character but when I tried to do so it came up with an error

 plrs.PlayerRemoving:Connect(function(plr)
for i, v in pairs(plr.Backpack:GetChildren()) do 
		if v:IsA("Tool") then
			table.insert(toolsname, v.Name)
		end
	end
	for i, v in pairs(plr.Character:GetChildren()) do 
		if v:IsA("Tool") then
			table.insert(toolsname, v.Name)
		end
	end

It came with the error "attempt to index nil with ‘GetChildren’ "

The player does not have a character when they’re leaving the game. Try detecting CharacterRemoving and save the character in a variable, and check if they are leaving the game in that event.

a player’s backpack is reset every time they spawn

a better solution would be cloning things to a player’s StarterGear and their backpack and then read from all of the starter gear

This is not the problem that im facing, i just couldnt access the player’s character when they are leaving

Thanks for your suggestion i will try it as soon as i get to my pc

1 Like

This code will save the tools the user has equipped, so you can use it in PlayerRemoving.

local ToolCache = {}

game.Players.PlayerAdded:Connect(function(player)
	ToolCache[player] = {}

	player.CharacterAdded:Connect(function(char)
		char.ChildAdded:Connect(function(obj)
			if obj:IsA("Tool") then
				ToolCache[player][obj.Name] = true
			end
		end)

		char.ChildRemoved:Connect(function(obj)
			if obj:IsA("Tool") then
				ToolCache[player][obj.Name] = nil
			end
		end)
	end)

	player.CharacterRemoving:Connect(function()
		table.clear(ToolCache[player])
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	print("Tools equipped:", ToolCache[player])
end)

you are trying to get the tools in the player’s inventory (their backpack and character)

presumably to save and load next visit. Most people manage their tools their own way by throwing them into starter gear

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