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:
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:
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.