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
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
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.