Attempt to index nil with 'FindFirstChild'

  1. What do you want to achieve? Detect if the handles object exists already. If it doesn’t, create a new one.

  2. What is the issue? It returns
    attempt to index nil with 'FindFirstChild'
    on the 12th line.

  3. What solutions have you tried so far? Tried rearranging the order of the checking, tried using FindFirstChildOfClass instead, didn’t work.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Main = player.PlayerGui:WaitForChild("Main")

local snapValue = 1
local partSelected = script.Parent.Parent.Selection.ClickSelected.Value


-- Event handler for mouse click
mouse.Button1Down:Connect(function()
	if Main.CurrentlySelected.Value == "Move" then
		if not partSelected:FindFirstChild("Handles") then
			local MoveHandle = Instance.new("Handles")
			MoveHandle = Instance.new("Handles")
			MoveHandle.Parent = player.PlayerGui
			MoveHandle.Adornee = partSelected
			MoveHandle.Style = Enum.HandlesStyle.Movement
			MoveHandle.Color3 = workspace[player.Name]["Values"]["PlayerColor"].Value.Color
			MoveHandle.Transparency = 0.2

			local origin = partSelected.CFrame

			MoveHandle.MouseDrag:Connect(function(a, b)
				if a.Value == 5 then
					partSelected.CFrame = origin + partSelected.CFrame.LookVector * (b - b % snapValue)
				elseif a.Value == 2 then
					partSelected.CFrame = origin + partSelected.CFrame.LookVector * -(b - b % snapValue)
				elseif a.Value == 1 then
					partSelected.CFrame = origin + partSelected.CFrame.UpVector * (b - b % snapValue)
				elseif a.Value == 4 then
					partSelected.CFrame = origin + partSelected.CFrame.UpVector * -(b - b % snapValue)
				elseif a.Value == 0 then
					partSelected.CFrame = origin + partSelected.CFrame.RightVector * (b - b % snapValue)
				elseif a.Value == 3 then
					partSelected.CFrame = origin + partSelected.CFrame.RightVector * -(b - b % snapValue)
				end
			end)

			MoveHandle.MouseButton1Up:Connect(function()
				origin = partSelected.CFrame
			end)
		else
			partSelected.FindFirstChildOfClass("Handles"):Destroy()
		end
	end
end)

This error means that the thing you are using :FindFirstChild() on is nil.

Add this line of code so that it doesn’t check when partSelected is nil

if not partSelected then return end

Edit: a better way to do this is to set partSelected as script.Parent.Parent.Selection.ClickSelected

and then get the value when the mouse is pressed

when you assign a variable to a value’s .Value, it doesnt change as the value changes. This is probably why the value was nil.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Main = player.PlayerGui:WaitForChild("Main")

local snapValue = 1
local partSelectedValue = script.Parent.Parent.Selection.ClickSelected -- dont assing value before the function because this is permanent and will not change even when the value changes


-- Event handler for mouse click
mouse.Button1Down:Connect(function()

    local partSelected = partSelectedValue.Value -- get the new and current value each time mouse is pressed

    if not partSelected then -- stop errors
      warn("partSelected value was not found")
      return
    end

	if Main.CurrentlySelected.Value == "Move" then
		if not partSelected:FindFirstChild("Handles") then
			local MoveHandle = Instance.new("Handles")
			MoveHandle = Instance.new("Handles")
			MoveHandle.Parent = player.PlayerGui
			MoveHandle.Adornee = partSelected
			MoveHandle.Style = Enum.HandlesStyle.Movement
			MoveHandle.Color3 = workspace[player.Name]["Values"]["PlayerColor"].Value.Color
			MoveHandle.Transparency = 0.2

			local origin = partSelected.CFrame

			MoveHandle.MouseDrag:Connect(function(a, b)
				if a.Value == 5 then
					partSelected.CFrame = origin + partSelected.CFrame.LookVector * (b - b % snapValue)
				elseif a.Value == 2 then
					partSelected.CFrame = origin + partSelected.CFrame.LookVector * -(b - b % snapValue)
				elseif a.Value == 1 then
					partSelected.CFrame = origin + partSelected.CFrame.UpVector * (b - b % snapValue)
				elseif a.Value == 4 then
					partSelected.CFrame = origin + partSelected.CFrame.UpVector * -(b - b % snapValue)
				elseif a.Value == 0 then
					partSelected.CFrame = origin + partSelected.CFrame.RightVector * (b - b % snapValue)
				elseif a.Value == 3 then
					partSelected.CFrame = origin + partSelected.CFrame.RightVector * -(b - b % snapValue)
				end
			end)

			MoveHandle.MouseButton1Up:Connect(function()
				origin = partSelected.CFrame
			end)
		else
			partSelected.FindFirstChildOfClass("Handles"):Destroy()
		end
	end
end)