Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

can't call a if statement to a for loop?

Asked by
idi112 0
6 years ago
Edited 6 years ago

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
2
Any errors? iamnoamesa 674 — 6y
0
No error's and I already have the lua debugger enabled. Nothing shows up idi112 0 — 6y

Answer this question