I'm trying to make an AI car. I use SetPrimaryPartCFrame on the unanchored car to the nearest highway node to rotate the car to the node. It looks fine on the server, but on the player's client it looks very choppy. Is there a reason to why this is happening?


local part = script.Parent

local function faceNearestBlock(part, maxDistance)
	local nodes = game.Workspace.HighwayNodes:GetChildren()
	local closestPart
	local minDistance = maxDistance
	local maxlanes = part:GetAttribute("MaxLanes")
	local randomlane = math.random(1,maxlanes)
	for _, partToFace in ipairs(nodes) do
		if partToFace.Name == "Lane" .. randomlane then
			local distance = (part.hood.CFrame.LookVector - partToFace.Position).magnitude
			closestPart = partToFace
			minDistance = distance
			script.Parent:SetAttribute("Lane", randomlane)
			script.Parent:SetAttribute("SwitchingLanes", true)
		end 
	end
	if closestPart then
		part:SetPrimaryPartCFrame(CFrame.new(part.PrimaryPart.Position,  Vector3.new(closestPart.Position.X, part.PrimaryPart.Position.Y, closestPart.Position.Z)))
	end
end

while wait(0.3) do
	faceNearestBlock(part, 5)
	script.Parent:SetAttribute("SwitchingLanes", false)
end

8 comments

0

Why not try usimg body movers like bodyangularvelocity

0

I want to use SetPrimaryPartCFrame.

0

what

0

????

0

use a localscript

0

Does it replicate to the server, though?

0

Nope, that doesn't solve the problem.

0

Sorry, accidentally pressed enter too early. It makes the vehicles act like the script doesn't exist at all.