Ok so I'm trying to get the closest brick to the player and [obviously] this isn't the best way, But I keep getting an error on line 14: 'attemp to compare number with string', To be honest I'm just starting to learn about magnitude and I really don't know what's wrong.
local walls = game.Workspace.Walls:GetChildren()
local chartorso = game.Players.LocalPlayer.Character.Torso
for i = 1, #walls do
walls[i].Name = "Wall"..i
end
function onKeyPress(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.E then
for i,v in pairs (walls) do
if math.ceil((chartorso.Position-walls[i].Position).magnitude) <= 20 then
local lowest = 20
for i,v in pairs (walls) do
if math.ceil((chartorso.Position-walls[i].Position).magnitude) < lowest then
lowest = v.Name
end
end
print("you're close to "..walls[i].Name)
break
end
end
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 comment
Why do you have two loops that use the same variables nested?