For loop passing through instances that aren't named Tooltips

I want the for loop to find all instances named Tooltip and make it so that when the users hover over its parent, the tooltip appears or else it will disappear.

The issue is that instances that aren’t named Tooltips passes through the if statements anyways.

for _, tooltip in pairs(mainUi:GetDescendants()) do
	if not tooltip.Name == "Tooltip" then continue end
	
	tooltip.Parent.MouseEnter:Connect(function()
		tooltip.Visible = true
	end)

	tooltip.Parent.MouseLeave:Connect(function()
		tooltip.Visible = false
	end)
end

I dont really see the point in using guard statements here if you have one if statement

for _, tooltip in pairs(mainUi:GetDescendants()) do
	if not tooltip.Name == "Tooltip" then 
		tooltip.Parent.MouseEnter:Connect(function()
			tooltip.Visible = true
		end)

		tooltip.Parent.MouseLeave:Connect(function()
			tooltip.Visible = false
		end)
	end
end
1 Like

Someone on Discord helped me, this works. Though, I don’t know why mine didn’t work.

for _, tooltip in pairs(mainUi:GetDescendants()) do
	if tooltip.Name ~= "Tooltip" then continue end
	
	tooltip.Parent.MouseEnter:Connect(function()
		tooltip.Visible = true
	end)

	tooltip.Parent.MouseLeave:Connect(function()
		tooltip.Visible = false
	end)
end

You forgot to remove not. (…)

1 Like

Since it wasn’t explained, the previous version didn’t work due to the order in which boolean expressions are evaluated.

not tooltip.Name is evaluated first, as tooltip.Name is truthy (not nil and not false) it evaluates to false, leaving you with the following.
if false == "Tooltip" then continue end
This obviously evaluates to false meaning the ‘continue’ statement is never executed.

1 Like

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