Performant alternative to tweens?

you can use cframe if you want to move the vehicles automatically

1 Like

How would this be anymore performance efficient than tweening?

Could you show a video of what the game is like? Have you done benchmarks to make sure that it is really tweening causing the issues?

I am unable to record a video of the game at the moment but here is the game link. Dodge Traffic! - Roblox

It could be the rapid cloning of the cars. You could try making a “loop” where you have a bunch of cars and tweens that get recycled over and over. You could also try posting your code in #help-and-feedback:code-review to get feedback and potential optimizations. Are these models or just a single meshpart?

Here

are you sure the performance drops are actually from the tweens playing? tween lag is usually from the sheer amount of un deleted loose tweens that are doing nothing other than taking up ram (you can directly use the :Delete() function on the completed tweens as they count as instances). also using cframes would actually be a better alternative performance wise than tweens.

Each vehicle is a model of several MeshParts welded together.

I am already deleting all tweens once they are done. What would be the best way to animate using CFrame? Simply using SetPrimaryPartCFrame every frame?

you can make a loop which adds the models position

you can move the entire welded assembly with a cframe if all of the side parts are not anchored and you don’t move it by setting the position, setting the position of one of two welded parts breaks the weld even if the other part is not anchored so use cframe instead of it.

1 Like

just adjust the anchored base parts cframe and all the un anchored welded parts will follow

1 Like

i recommend u to use “BodyVelocity” i would do that with it. its easy to use all u need to do is adding a “BodyVelocity” inside a part. then set the velocity to…
part.CFrame.LookVector * 50
hope this helps…
edit: asol parts must be unanchored

Physics are much worse on performance than using CFrames. I was using BodyVelocities previously and they were horrible.

they can be horribe sometimes. did you tryed to use CFrame:Lerp()?

edit, this may need a goal position… and parts should be anchored

1 Like

I tried this solution and before I was getting activity of 1-2%, now it’s upwards of 17%.image
This is the code I am using.

local function RenderStepped(DT)
	for i, v in pairs(MovingCars) do
		if not v.Parent then
			table.remove(MovingCars,i)
			continue
		end
		v.PrimaryPart.CFrame *= CFrame.new(0,0,-(v.Lane.Value:GetAttribute("Speed")*DT))
	end
end

first, you should only reserve code for render stepped if it truly needs to be there, an example of this would be camera movement and first person arms(fps game), try putting it in heartbeat instead. id also recommend only moving cars you can actually see

You can lerp the CFrame , so that you won’t have to create a lot of tween objects and play.
Example :

local x = 2
local TargetCFrame = ...
local function Update(delta)
  Part.CFrame = Part.CFrame:lerp(TargetCFrame,delta * x)
end
RunService.RenderStepped:Connect(Update)

This is a small example on how you could do it.

1 Like

if you where to only move cars you can actually see then you would have to save the z position as a value for each cars instead of adjusting the cframe each frame. so like each car has a forward position value and you add to that value every frame, then you set the cframe to CFrame.new(xpos,ypos,currentZdistance) as an example.

1 Like

With a range of only 800 studs it still has high activity.
image
You can see cars popping in and out so the range would be even higher. Here’s my code.

local function Heartbeat(DT)
	for i, v in pairs(MovingCars) do
		if not i.Parent then
			MovingCars[i] = nil
			continue
		end
		local Negative = 1
		if i.Lane.Value.Parent.Name == "Easy" then
			Negative = -1
		end
		MovingCars[i] -= (i.Lane.Value:GetAttribute("Speed")*DT) * Negative
		if Plr.Character and Plr.Character.PrimaryPart then
			local Magnitude = (Vector3.new(i.Body.HitBox.Position.X,i.Body.HitBox.Position.Y,MovingCars[i])-Plr.Character.PrimaryPart.Position).Magnitude
			if Magnitude <= Range then
				i.PrimaryPart.CFrame = CFrame.new(i.PrimaryPart.Position.X,i.PrimaryPart.Position.Y,MovingCars[i]) * CFrame.Angles(math.rad(i.PrimaryPart.Orientation.X),math.rad(i.PrimaryPart.Orientation.Y),math.rad(i.PrimaryPart.Orientation.Z))
			end
		end
	end
end