Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Comparing magnitude with a string help?

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

0

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

1 answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.