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.
01 | local walls = game.Workspace.Walls:GetChildren() |
02 | local chartorso = game.Players.LocalPlayer.Character.Torso |
03 |
04 | for i = 1 , #walls do |
05 | walls [ i ] .Name = "Wall" ..i |
06 | end |
07 |
08 | function onKeyPress(inputObject, gameProcessedEvent) |
09 | if inputObject.KeyCode = = Enum.KeyCode.E then |
10 | for i,v in pairs (walls) do |
11 | if math.ceil((chartorso.Position-walls [ i ] .Position).magnitude) < = 20 then |
12 | local lowest = 20 |
13 | for i,v in pairs (walls) do |
14 | if math.ceil((chartorso.Position-walls [ i ] .Position).magnitude) < lowest then |
15 | lowest = v.Name |
This is because you have turned "lowest" into a string itself within the loop. Then the next time the loop goes through, it tries to compare a number with a string because you changed lowest into a string. Try using a different variable for "lowest" in your loop, re-define lowest at the beginning of the loop as a number, or just get rid of that "lowest=v.Name" in general.