I cannot find out why it is jittering like this, it would work fine the first 10 to 15 strafe, 20 if i was lucky. it just happens, and i cannot tell WHY.
normal, working strafing:
bugged strafing:
Module script:
local ServerStorage = game:GetService("ServerStorage")
local Strafe = {}
local function getAdjacentVertices(NPCPosition, PlayerPosition, VertexCount, Radius)
local center = PlayerPosition
local npc = NPCPosition
local direction = (npc - center)
local currentAngle = math.atan2(direction.Z, direction.X)
local angleStep = (2 * math.pi) / VertexCount
local angle1 = currentAngle + angleStep
local angle2 = currentAngle - angleStep
local vertex1 = center + Vector3.new(math.cos(angle1), 0, math.sin(angle1)) * Radius
local vertex2 = center + Vector3.new(math.cos(angle2), 0, math.sin(angle2)) * Radius
return vertex1, vertex2
end
local function waitForArrival(RootPart, Destination, maxTime, tolerance)
maxTime = maxTime or 2
tolerance = tolerance or 0.5
local timer = 0
while timer < maxTime do
local distance = (RootPart.Position - Destination).Magnitude
if distance <= tolerance then
return true -- arrived
end
timer += task.wait()
end
return false -- timed out
end
function Strafe.Strafe(Character, Target, StrafeData)
local Vertex = StrafeData.Vertex
local Direction = StrafeData.Direction
local Radius = StrafeData.Radius
local Steps = StrafeData.Steps
print(StrafeData.Steps, StrafeData.Direction)
local Humanoid = Character.Humanoid
if Character:GetAttribute("Strafe") == true then
Character:SetAttribute("Strafe", false)
Humanoid:MoveTo(Character.HumanoidRootPart.Position)
end
Character:SetAttribute("Strafe", true)
for i = 1, Steps do
--print(Direction, Steps)
local Left, Right = getAdjacentVertices(Character.HumanoidRootPart.Position, Target.HumanoidRootPart.Position, Vertex, Radius)
local Destination
if Direction == 0 then
Destination = Vector3.new(Left.X, Character.HumanoidRootPart.Position.Y ,Left.Z)
else
Destination = Vector3.new(Right.X, Character.HumanoidRootPart.Position.Y ,Right.Z)
end
warn("NewCalc")
workspace.Terrain.Target.Position = Destination
Humanoid:MoveTo(Destination)
local Conenction
local StepReached = false
local Timeout = 0
local success = waitForArrival(Character.HumanoidRootPart, Destination, 2, 1)
if not success then
warn("Strafe step timeout")
end
warn("StrafePassed")
Humanoid:MoveTo(Character.HumanoidRootPart.Position)
end
Character:SetAttribute("Strafe", false)
end
--[[
StrafeData = {
Vertex = 20,
Direction = math.random(1,0),
Radius = 6,
Steps = math.random(1, 20)
}]]
return Strafe
please help.