#MousePosition

1 messages · Page 1 of 1 (latest)

twin trellis
#

the problem is
"Generator" is a part right, i wanted like it print the nearest generator with my mouse position
but for some reason even when i change my mouse position it always print the same generator

wintry quail
#

Mouse.Button1Down:Connect(function()
if Activate then
local closestGenerator = nil
local shortestDistance = math.huge
local MousePos = Mouse.Hit.Position
print("MousePos:", MousePos) -- Debug

    for _, Generator in pairs(Generators.Generator:GetChildren()) do
        for _, GeneratorUI in pairs(Generator:GetChildren()) do
            if GeneratorUI:IsA("BillboardGui") then -- Simplified check
                local Distance = (MousePos - Generator.Position).Magnitude
                print("Generator:", Generator.Position, "Distance:", Distance) -- Debug
                if Distance < shortestDistance then
                    shortestDistance = Distance
                    closestGenerator = Generator
                end
            end
        end
    end

    if closestGenerator then
        print("Closest:", closestGenerator)
    end
end

end)