The red is how it’s supposed to be and the green is how it is right now.
Script:
local function createCircle(rootPart, targetRoot)
table.clear(points)
local distance = math.round((rootPart.Position - targetRoot.Position).Magnitude)
local radius = distance / 2
local numberOfParts = distance
local centerPosition = (rootPart.Position + targetRoot.Position) / 2
centerPosition = vector.create(centerPosition.X, 2, centerPosition.Z)
for i = 1, numberOfParts do
local angle = (math.pi * 2 / numberOfParts) * i
local x_pos = (math.cos(angle) * radius)
local z_pos =(math.sin(angle) * radius)
table.insert(points, centerPosition + vector.create(x_pos, 0, z_pos))
end
return #points > 0, numberOfParts
end
local horizontalDir = 0
local success, numberOfParts = createCircle(rootPart, workspace.Rig:WaitForChild("HumanoidRootPart"))
if not success then return end
if action == "Right" then
horizontalDir = -1
elseif action == "Left" then
horizontalDir = 1
end
if horizontalDir ~= 0 then
local closestPoint = nil
local closestDistance = 100
local nextIndex = 0
for i, point in ipairs(points) do
if (rootPart.Position - point).Magnitude < closestDistance then
closestPoint = point
closestDistance = (rootPart.Position - point).Magnitude
nextIndex = i + horizontalDir
end
end
local nextPoint = points[nextIndex]
if not nextPoint then
nextPoint = points[#points]
end
if not nextPoint or not closestPoint then return end
direction = (nextPoint - closestPoint).Unit * 1
end
createVelocity(rootPart, direction)
At first I use look vector and right vector, but since that wasn’t a circle it caused the NPC to move further away from the point. Then I used .Unit, but it gets closer to the center point for some reason. I also tried increasing the number of parts variable, but that doesn’t work too.
Looks like this is using (nextPoint - closestPoint).Unit * 1 as the direction, but you never update closestPoint after moving, so each frame your NPC is recalculating a vector that is slightly shorter than the actual radius, effectively spiraling inward.
Blind shot at it, fully event-driven.
ServerScript
--ServerScript in ServerScriptService
local TweenService = game:GetService("TweenService")
local horizontalDir = 1
local currentIndex = 1
local points = {}
local speed = 2
local rootPart = workspace.NPC.HumanoidRootPart
local target = workspace.Player.HumanoidRootPart
local function createCircle(rootPart, targetRoot)
table.clear(points)
local distance = (rootPart.Position - targetRoot.Position).Magnitude
local radius = distance / 2
local numberOfParts = 36
local center = Vector3.new((rootPart.Position.X + targetRoot.Position.X)/2, 2,
(rootPart.Position.Z + targetRoot.Position.Z)/2)
for i = 1, numberOfParts do
local angle = (math.pi * 2 / numberOfParts) * i
local x = math.cos(angle) * radius
local z = math.sin(angle) * radius
table.insert(points, center + Vector3.new(x, 0, z))
end
end
createCircle(rootPart, target)
local function moveToNextPoint()
local nextIndex = currentIndex + horizontalDir
if nextIndex > #points then nextIndex = 1 end
if nextIndex < 1 then nextIndex = #points end
currentIndex = nextIndex
local goal = {CFrame = CFrame.new(points[currentIndex])}
local tween = TweenService:Create(rootPart,
TweenInfo.new(speed, Enum.EasingStyle.Linear), goal)
tween.Completed:Connect(moveToNextPoint)
tween:Play()
end
moveToNextPoint()
Here’s basically the whole script. I tried cleaning it up before sorry. @SeargentAUS. @2112Jay I’ll try implenting right now thank you.
Script:
local player = game:GetService("Players").LocalPlayer
local points = {}
local function createCircle(rootPart, targetRoot)
table.clear(points)
local distance = math.round((rootPart.Position - targetRoot.Position).Magnitude)
local radius = distance / 2
local numberOfParts = distance
local centerPosition = (rootPart.Position + targetRoot.Position) / 2
centerPosition = vector.create(centerPosition.X, 2, centerPosition.Z)
for i = 1, numberOfParts do
local angle = (math.pi * 2 / numberOfParts) * i
local x_pos = (math.cos(angle) * radius)
local z_pos =(math.sin(angle) * radius)
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(1, 1, 1)
part.Transparency = 0
part.Position = centerPosition + vector.create(x_pos, 0, z_pos)
part.BrickColor = BrickColor.Random()
game:GetService("Debris"):AddItem(part, 1)
part.Parent = workspace
table.insert(points, centerPosition + vector.create(x_pos, 0, z_pos))
end
return #points > 0, numberOfParts
end
local function createVelocity(rootPart, direction, distance)
local velocity = rootPart:WaitForChild("RootAttachment"):WaitForChild("LinearVelocity")
velocity.Enabled = true
velocity.VectorVelocity = direction * distance
task.delay(1, function()
velocity.Enabled = false
velocity.VectorVelocity = vector.zero
end)
end
local function onInput(actionName, inputState)
local character = player.Character
local rootPart = character:WaitForChild("HumanoidRootPart")
local direction = vector.zero
local horizontalDir = 0
local distance = nil
local success, numberOfParts = createCircle(rootPart, workspace.Rig:WaitForChild("HumanoidRootPart"))
if not success then return end
if actionName == "Right" then
horizontalDir = -1
elseif actionName == "Left" then
horizontalDir = 1
end
if horizontalDir ~= 0 then
local closestPoint = nil
local closestDistance = 100
local nextIndex = 0
for i, point in ipairs(points) do
if (rootPart.Position - point).Magnitude < closestDistance then
closestPoint = point
closestDistance = (rootPart.Position - point).Magnitude
nextIndex = i + horizontalDir
end
end
local nextPoint = points[nextIndex]
if not nextPoint then
nextPoint = points[#points]
end
if not nextPoint or not closestPoint then return end
direction = (nextPoint - closestPoint).Unit * 1
distance = (nextPoint - closestPoint).Magnitude
end
createVelocity(rootPart, direction, distance)
end
ContextActionService:BindAction("Right", onInput, true, Enum.PlayerActions.CharacterRight)
ContextActionService:BindAction("Left", onInput, true, Enum.PlayerActions.CharacterLeft)
Sorry for the late response. I solved it though. It had to do with the create velocity function, since I tried using humanoid:MoveTo() and it worked. I tried setting the velocity to the distance * 10, but it overshot that’s why it went too close thanks.