So im trying to calculate the closest object to the player's position. And if the distance to the closest object is less than 10 studs. The only problem is that the for loop stops whenever I go in the 10 stud range. What should I do to fix this?
Here's the code:
game.Workspace:WaitForChild(game.Players.LocalPlayer.Name) local children = workspace.allguns:GetChildren() local character = game.Players.LocalPlayer.Character local magnitude = nil local closestDist, closestGun = math.huge local closestGun = nil while wait(2) do for index, child in pairs(workspace.allguns:GetChildren()) do local magnitude = (child.Hitbox.Position - character.HumanoidRootPart.Position).magnitude if magnitude < closestDist then closestGun = child closestDist = magnitude print(closestGun) if closestDist < 10 then closestGun.Barrel.BillboardGui.Enabled = true else closestGun.Barrel.BillboardGui.Enabled = false end end end end