Problem with tile movement system

I’ve been getting this error when trying to run the script below.

Players.PizzaArmy333.PlayerScripts.Hitbox:43: attempt to compare Vector2 <= Vector2

That’s not the real problem, the real problem is how do I see if the spot the NPC needs to move to is under or equal to the max movement distance? I need a way to check if the distance is valid on line 43

The script (Local script in StarterPlayerScripts)

local units = game.Workspace.Units:GetChildren()
local move = game.ReplicatedStorage.Remotes.Move

local selected = false

for _, unit in ipairs(units) do
	if unit:IsA("Model") and unit:FindFirstChildWhichIsA("Model") then
		local char = unit:FindFirstChildWhichIsA("Model")
		
		local hitbox = char:WaitForChild("Hitbox")
		local click = hitbox:WaitForChild("ClickDetector")
		local data = unit:FindFirstChild("Data")

		if click and data then
			click.MouseHoverEnter:Connect(function(plr)
				data.Extra.SelectionBox.Visible = true
			end)

			click.MouseHoverLeave:Connect(function(plr)
				if selected == false then
					data.Extra.SelectionBox.Visible = false
				end
			end)
			
			click.MouseClick:Connect(function(plr)
				selected = not selected
				data.Extra.SelectionBox.Visible = true
			end)
			
			local mouse = game.Players.LocalPlayer:GetMouse()
			
			mouse.Button1Down:Connect(function()
				if mouse.Target.Parent.Name == "Tiles" and mouse.Target:IsA("MeshPart") then
					local targetTile = mouse.Target
					local targetCords = targetTile:GetAttribute("Cords")
					
					local currentTile = data.CurrentTile
					local currentCords = currentTile:GetAttribute("Cords")
					
					--TODO move to server script
					local maxMove = data:GetAttribute("MoveRange")
					
					if targetCords <= maxMove and data.Owner.Value == game.Players.LocalPlayer.Name then
						char.HumanoidRootPart.Position = targetTile.Position + Vector3.new(0, 0.65, 0)
					end
				end
			end)
		end
	end
end

A custom Vector2 coordinates system is used for each tile.
tiles

I can provide more info (screenshots etc) of any more information you need. Thank you!

2 Likes

I think you made some miscalculation here, this is might be a fixed version (remove .Magnitude if its still not working)

if (targetCords - currentCords).Magnitude <= maxMove and data.Owner.Value == game.Players.LocalPlayer.Name then
3 Likes

Got this with and without the .Magnitude

 Players.PizzaArmy333.PlayerScripts.Hitbox:43: invalid argument #2 (Vector2 expected, got nil)

(sorry late response fire alarm went off)

3 Likes

can you print before that what targetcords and currentCords are? (and maxMove)

3 Likes

Hmmmm…

I got this when running the print:

Players.PizzaArmy333.PlayerScripts.Hitbox:42: attempt to concatenate string with nil
print(targetCords.." "..currentCords)

MaxMove is (1,1)

1 Like

you gotta do a comma

print(targetCords, currentCords)

added maxmove, heres what i got

3, 1 nil 1, 1

I found the issue from a related post someone sent

I think in your Code it would be this correction then:

if (targetCords.X <= maxMove.X) and (targetCords.Y <= maxMove.Y) and data.Owner.Value == game.Players.LocalPlayer.Name then

It’s printing the same thing, but the error is now gone.

I’m gonna fiddle with this and I’ll notify if I fix it

I made a slight mistake, please use the current code I edited.

The same thing is happening, it’s printing the target cords and max move correctly, but the current cords are nil for some reason

Yea, it’s probably you set something wrong.

But what? theres nothing setting it to nil and nothing is set to nil

Have you ever set the Attribute for the CurrentTile/Cords

CurrentTile is a value set to default of the square under it specifically, and the script is trying to get CurrentCords from an attribute in the CurrentTile

Like Value in an attribute or value like a num value?

currenttile is an object value

I havent made the part where it changes when it gets to its destination

So if I understand you correctly then this should help?

local currentCords = currentTile.Value

No, it would have to be

local currentCords = currentTile:GetAttribute("Cords")

Maybe you have misspelt Cords with something else

1 Like