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

Comparing magnitude with a string help?

Asked by
NotSoNorm 777 Moderation Voter
9 years ago

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.

01local walls = game.Workspace.Walls:GetChildren()
02local chartorso = game.Players.LocalPlayer.Character.Torso
03 
04for i = 1, #walls do
05    walls[i].Name = "Wall"..i
06end
07 
08function 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
View all 25 lines...
0
Why do you have two loops that use the same variables nested? BlueTaslem 18071 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

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.

Ad

Answer this question