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

Unequip player tools while leaving?

Hello, I need to Unequip player tools while he leaving the game but I got error :(

game.Players.PlayerRemoving:connect(function(Player)
	Player.Character.Humanoid:UnequipTools()
	print("ToolsUnequiped")
end)

Output: attempt to index field 'Character' (a nil value) line:2

2 comments

0

The error is saying the character doesn't exist, and that's because you tried to reference it after the player has left (or technically is) leaving the game. I'm curious as to why you want to try and unequip a player's tool when they try to leave the game?

0

because when player leave while equipping the tool it doesn't save equipping tool, and saved another unequipping tools.

1 answer

The PlayerRemoving event fires only after the player's character is removed and the player's parent is set to null.

However, Player Instances also have an event called CharacterRemoving which you can use in conjunction with the PlayerAdded event to remove the tools from the player when they leave the game.

Also use :WaitForChild() to make sure that the Humanoid still exists.

Revised Server Script

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterRemoving:Connect(function(Character)
		Character:WaitForChild("Humanoid"):UnequipTools()
		print("ToolsUnequiped")
	end)
end)

1 comment

0

tysm

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