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.

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)
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