Code issues - Attempt to Index Nil Error in AreaTouched Script

Hello! I’m having issues trying to touch my part, and I’m getting the following error. Does anyone know how I can fix it? :pray:

I need to make it so that when the part is touched and the player is on Team 2, the GUI should be shown, and if not, it should be hidden.

the error:

  22:45:16.896  Workspace.Elements.Deliverables.table_1.AreaTouched:8: attempt to index nil with 'FindFirstChildOfClass'  -  Servidor - AreaTouched:8

server script:

local partTouched = script.Parent.Parent.table_1

if not partTouched then
	warn("PartTouched is nil. Check the script hierarchy.")
	return
end

local playerGuiToShow = game.Players.LocalPlayer:FindFirstChildOfClass("PlayerGui"):WaitForChild("addToolToTable_UI")

partTouched.Touched:Connect(function(hit)
	local character = hit.Parent
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")

	if humanoid and humanoid:IsA("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(character)

		if player then
			if player.Team.Name == "Team 2" then
				-- Show the specified PlayerGui when a player from Team 2 touches the part
				if playerGuiToShow then
					playerGuiToShow.Enabled = true
					print("Player " .. player.Name .. " from Team 2 touched the part. Showing PlayerGui.")
				else
					warn("PlayerGui 'addToolToTable_UI' not found.")
				end
			else
				-- Your code here for when a player from a different team touches the part
				print("Player " .. player.Name .. " is not from Team 2.")
			end
		end
	end
end)
4 Likes

No problem at all! I was able to solve it. Lol. :sweat_smile:

local partTouched = script.Parent.Parent.table_1

if not partTouched then
	warn("PartTouched is nil. Check the script hierarchy.")
	return
end

partTouched.Touched:Connect(function(hit)
	local character = hit.Parent
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")

	if humanoid and humanoid:IsA("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(character)

		if player then
			if player.Team.Name == "Team 1" then
				-- Show the specified PlayerGui when a player from Team 1 touches the part
				local playerGuiToShow = player:FindFirstChild("PlayerGui"):FindFirstChild("addToolToTable_UI")

				if playerGuiToShow then
					playerGuiToShow.Enabled = true
					print("Player " .. player.Name .. " from Team 1 touched the part. Showing PlayerGui.")
				else
					warn("PlayerGui 'addToolToTable_UI' not found for Player " .. player.Name)
				end
			else
				-- Your code here for when a player from a different team touches the part
				print("Player " .. player.Name .. " is not from Team 1.")
			end
		end
	end
end)

partTouched.TouchEnded:Connect(function(hit)
	local character = hit.Parent
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")

	if humanoid and humanoid:IsA("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(character)

		if player then
			-- Hide the specified PlayerGui when a player stops touching the part
			local playerGuiToShow = player:FindFirstChild("PlayerGui"):FindFirstChild("addToolToTable_UI")

			if playerGuiToShow then
				playerGuiToShow.Enabled = false
				print("Player " .. player.Name .. " stopped touching the part. Hiding PlayerGui.")
			end
		end
	end
end)
1 Like

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