My AI Car Script makes delay. Please help me

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I made script which detect Part(named Goal) and check position difference between part and a front vector of car. Wheels are composed of Hinge Constraints. Calculate the angle and using HingeConstraint.TargetAngle. Car Speeds are controlled by HingeConstraint.AngularVelocity.

  1. What is the issue? Include screenshots / videos if possible!
    https://youtu.be/Rjeu8uoKaoU

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I rewrite other scripts beacause of this problem. I check that there is no laggy when I disable this script. I’m pretty sure this scripts make problem. I clone and spawn 12 Cars on my workspace.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

--[[Services]]
local serverStorage=game:GetService("ServerStorage")
local runService=game:GetService("RunService")
local replicatedStorage=game:GetService("ReplicatedStorage")
local Players=game:GetService("Players")

--[[Valuables]]
local car=script.Parent.Parent
local vector=car:WaitForChild("Vector")
local frontVector=vector.FrontVector
local rearVector=vector.RearVector

--[[Folder]]
local lineFolder=workspace:WaitForChild("CarLine")
local statsFolder=car:WaitForChild("Stats")
--local itemFolder=serverStorage:WaitForChild("Items")
local itemFolder=replicatedStorage:WaitForChild("Item")
local dieFolder=replicatedStorage:WaitForChild("Die")

--[[Lines]]
local line=statsFolder.Line
local chosenLine=lineFolder:FindFirstChild(line.Value)
local goal=itemFolder.Goal

local firstTarget=chosenLine.Start

--[[Settings]]
local goalClone=goal:Clone()
goalClone.Parent=workspace
goalClone.CFrame=firstTarget.CFrame

local debounce=false
local goalNum=0

--[[Remotes]]
local dieCar=dieFolder.DieCar

--[[Constraints Value]]
local constraints = script.Parent.Parent:WaitForChild("Constraints")
local rightCons = constraints.Right 
local leftCons = constraints.Left

local rearRight = constraints.RR
local rearLeft = constraints.RL

local frontRear = constraints.FR
local frontLeft = constraints.FL

--Settings
local driveSpeed=statsFolder.DriveSpeed
local currentSpeed=statsFolder.CurrentSpeed

rearRight.AngularVelocity = driveSpeed.Value
rearLeft.AngularVelocity = driveSpeed.Value
frontRear.AngularVelocity = driveSpeed.Value
frontLeft.AngularVelocity = driveSpeed.Value

currentSpeed.Changed:Connect(function(newVal)
	rearRight.AngularVelocity = tonumber(newVal)
	rearLeft.AngularVelocity = tonumber(newVal)
	frontRear.AngularVelocity = tonumber(newVal)
	frontLeft.AngularVelocity = tonumber(newVal)
end)

--HumanVector Functions
rearVector.Touched:Connect(function(hit)
	local humanoid=hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health=0
		local player=Players:GetPlayerFromCharacter(hit.Parent)
		dieCar:FireClient(player)	
	end
end)

--FrontVector Functions
frontVector.Touched:Connect(function(part)
	if part.Name=="CrossWalkRed" then
		currentSpeed.Value=	driveSpeed.Value
	elseif part.Name=="CrossWalkGreen" then
		currentSpeed.Value="0"	
	elseif part.Name=="RearVector" then
		currentSpeed.Value="0"
	end
end)

frontVector.TouchEnded:Connect(function(part)
	if part.Name=="RearVector" then
		local balanceSpeed=part.Parent.Parent.Stats:FindFirstChild("DriveSpeed")
		currentSpeed.Value=tostring(balanceSpeed.Value)
	end
end)

runService.Heartbeat:Connect(function()
	local posDiffer=(goalClone.Position - frontVector.Position).Magnitude
	if posDiffer<10 then
		goalNum+=1
		local nextGoal=chosenLine:FindFirstChild(goalNum)
		if nextGoal then
			goalClone.Position=nextGoal.Position
		elseif nextGoal==nil then
			goalClone:Destroy()		
			car:Destroy()
		end
	end
	local PositionDifference = (goalClone.Position - frontVector.Position).Unit	

	local GoalFrontVector = frontVector.CFrame.LookVector

	local DotProduct = PositionDifference:Dot(GoalFrontVector)

	local TargetAngle = math.deg(math.acos(DotProduct)) - 90

	if TargetAngle < 90 then
		TargetAngle = TargetAngle
	elseif TargetAngle > 90 then
		TargetAngle = 90 - TargetAngle
	end

	rightCons.TargetAngle = TargetAngle
	leftCons.TargetAngle =  TargetAngle

end)