I am trying to make a system that moves the player just like click to move, but with 16x16 tiles, the best result i got with AI was this:
Code:
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local gridSize = 16
local cooldownActive = false -- Variable to track cooldown state
-- Create a part to represent the grid cell
local gridCell = Instance.new("Part")
gridCell.Size = Vector3.new(gridSize, 0.1, gridSize)
gridCell.Anchored = true
gridCell.CanCollide = false
gridCell.Transparency = 0.5
gridCell.Parent = Workspace
local function getNearestGridPosition(position)
local x = math.floor(position.X / gridSize + 0.5) * gridSize
local z = math.floor(position.Z / gridSize + 0.5) * gridSize
return Vector3.new(x, position.Y, z)
end
local function isClosestGrid(targetPosition)
local playerPosition = humanoidRootPart.Position
local nearestGridPosition = getNearestGridPosition(playerPosition)
local distance = (nearestGridPosition - targetPosition).Magnitude
local dx = math.abs(nearestGridPosition.X - targetPosition.X)
local dz = math.abs(nearestGridPosition.Z - targetPosition.Z)
return (dx == 0 and dz <= gridSize) or (dz == 0 and dx <= gridSize)
end
local function startCooldown()
cooldownActive = true
gridCell.BrickColor = BrickColor.new("Bright red")
task.wait(6) -- 6 seconds cooldown
cooldownActive = false
end
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessed and not cooldownActive then
local mouse = player:GetMouse()
local targetPosition = getNearestGridPosition(mouse.Hit.Position)
if isClosestGrid(targetPosition) and targetPosition ~= getNearestGridPosition(humanoidRootPart.Position) then
humanoid:MoveTo(targetPosition)
startCooldown() -- Start cooldown after moving
end
end
end)
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local mouse = player:GetMouse()
local hoverPosition = getNearestGridPosition(mouse.Hit.Position)
gridCell.Position = hoverPosition + Vector3.new(0, 0, 0) -- Slightly above the ground
if cooldownActive then
gridCell.BrickColor = BrickColor.new("Bright red")
elseif isClosestGrid(hoverPosition) and hoverPosition ~= getNearestGridPosition(humanoidRootPart.Position) then
gridCell.BrickColor = BrickColor.new("Bright yellow")
else
gridCell.BrickColor = BrickColor.new("Bright red")
end
end
end)
The problem is that the player can go to the tile it’s already on top of, i don’t want that but i can’t manage to fix it.

The grids around the player look like that, but i am trying to make this:
