FindFirstChildOfClass is not working

I am trying to make a script that deletes a tool from the player’s character and its cloned Gui from the playerGui if a specific condition was true but the problem is that when I try to test the script it was just not working or doing anything even if I equip a tool in my inventory with the conditions.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local playerGui = player:WaitForChild("PlayerGui")
local ToolsGui = playerGui:WaitForChild("Tools")
local UserInputService = game:GetService("UserInputService")
local CharTool = char:FindFirstChildOfClass("Tool")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Backspace and CharTool and CharTool.CanBeDropped == true and CharTool:FindFirstChildOfClass("ScreenGui") then
		local ToolGui = CharTool:FindFirstChildWhichIsA("ScreenGui")
		print(ToolGui.Name)
		print(CharTool.Name)
		if ToolGui then
			ToolsGui:FindFirstChild(ToolGui.Name):Destroy()
			CharTool:Destroy()
			print("Item Gui and Item was Removed!")
		end
	end
end)

Maybe try:

for i,v in pairs (char:GetChildren) do
   If v:IsA("Tool") then
      local CharTool = v
   end
end
1 Like
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local playerGui = player:WaitForChild("PlayerGui")
local ToolsGui = playerGui:WaitForChild("Tools")
local UserInputService = game:GetService("UserInputService")
local CharTool = char:FindFirstChildOfClass("Tool")
local ToolGui = CharTool:WaitForChild("ScreenGui")

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	if input.KeyCode == Enum.KeyCode.Backspace and CharTool and CharTool.CanBeDropped and ToolGui then
		ToolsGui:FindFirstChild(ToolGui.Name):Destroy()
		CharTool:Destroy()
	end
end)
1 Like

When is this script running? Is it possible that the tool is not in the Character at the time of this script trying to define it?

If you want to look for a Tool every time you press backspace, then I’d define CharTool inside of the Input function, like so:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local playerGui = player:WaitForChild("PlayerGui")
local ToolsGui = playerGui:WaitForChild("Tools")
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Backspace then
		local CharTool = char:FindFirstChildOfClass("Tool")
		if CharTool and CharTool.CanBeDropped == true and CharTool:FindFirstChildOfClass("ScreenGui") then
			local ToolGui = CharTool:FindFirstChildWhichIsA("ScreenGui")
			print(ToolGui.Name)
			print(CharTool.Name)
			if ToolGui then
				ToolsGui:FindFirstChild(ToolGui.Name):Destroy()
				CharTool:Destroy()
				print("Item Gui and Item was Removed!")
			end
		else
			print("No Tool Found.")
		end	
	end
end)

Bear in mind, FindFirstChildOfClass does not wait for a tool to appear, but rather looks only once, however since Tools are interchangeable it’s best to check every time you try to unequip a tool (press backspace) that one actually exists.

Hopefully this helps :slight_smile:

1 Like