Problem with movement system

I’m working on what a turn based RTS’s movement system would look like and encountered an issue.

The NPC will only go to the circled hexes:
hexes


As you can see, it refuses to go to the farthest space. This is because of a check in the ServerScript that receives the Move remote event and prevents it from going farther than 1 hex every move, but instead it keeps it in the hexes around it’s origin.

The output:
output
There is no Success printed after trying to move to the 4, 1 hex, which means it failed checks in the ServerScript below

The script that handles getting the target square and original position is here 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.Value
					local currentCords = data:GetAttribute("CurrentCords")
					
					local maxMove = data:GetAttribute("MoveRange")
					
					game.ReplicatedStorage.Remotes.Move:FireServer(targetTile, targetCords, currentTile, currentCords, maxMove, char, data)
					print(targetCords)
				end
			end)
		end
	end
end

The receiving end of the system, which handles checking and pathfinding to the square.

local pathfindingservice = game:GetService("PathfindingService")

game.ReplicatedStorage.Remotes.Move.OnServerEvent:Connect(function(plr, targetTile, targetCords, currentTile, currentCords, maxMove, char, data)
	if targetCords.X - maxMove.X -1 <= maxMove.X and targetCords.Y - maxMove.Y -1 <= maxMove.Y and data.Owner.Value == plr.Name then
		print("success")
	local path = pathfindingservice:CreatePath()

	local success, errorMessage = pcall(function()
		path:ComputeAsync(char.PrimaryPart.Position, targetTile.Position + Vector3.new(0, 0.75, 0))
	end)

	if success then
		local waypoints = path:GetWaypoints()

		for i, waypoint in ipairs(waypoints) do
			char.Humanoid:MoveTo(waypoint.Position)

			char.Humanoid.MoveToFinished:Wait()
		end
	else
		warn("Failed to compute path:", errorMessage)
	end
	end
	
	data.CurrentTile.Value = targetTile
	currentCords = targetTile:GetAttribute("Cords")
end)

An overview of the whole NPC


A custom coordinates system is in place with Vector2 attributes. Each hex has it’s own coordinates attribute.

Basically, the check in the ServerScript to make sure it is a valid move is stopping the NPC from moving farther than 1 hex around it’s origin. I need a way to prevent this, and am open to any ideas.

1 Like

You could do this way easier with math. also using pathfinder service is unecessary, because as i aforementioned its easier to do so with math and walkToPoint. If you need any help on the math part, reply to my message, ill try responding as fast as i can!

I’m kind of stinky at math, if you can help me ill take it

also the pathfinding is needed because there is sometimes obstaces like mountains to avoidd

roblox path finding is more for a 3d world . it is better to use some pathfinding algorithms bit easier to just move the rig over without pathfinding . btw I recommend not using humanoid to move the rig as it will cause insane lag . use velocity instead

do you know how i can fix the actual issue by any chance?

i fixed it by making the first and a or and removing the -1

You should really consider using OOP here instead of storing everything as instance values.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.