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.
just adjust the anchored base parts cframe and all the un anchored welded parts will follow
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
I tried this solution and before I was getting activity of 1-2%, now it’s upwards of 17%.
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.
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.
With a range of only 800 studs it still has high activity.
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
try using values a bit more like instead of doing part.item.anotheritem.Position.X then part.item.anotheritem.Position.Y. create a variable of part.item.anotheritem.Position then read off that variable. you could do this for the primary part value, the speed attribute, and the current position
How would this change anything?
reading values of instances takes up time, this time becomes more apparent the bigger your loop becomes.
This really hasn’t changed anything.
Here’s the 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 MovingCars[i]["Lane"].Parent.Name == "Easy" then
Negative = -1
end
MovingCars[i]["Value"] -= (MovingCars[i]["Lane"]:GetAttribute("Speed")*DT) * Negative
if Plr.Character and Plr.Character.PrimaryPart then
local Magnitude = (Vector3.new(MovingCars[i]["X"],MovingCars[i]["Y"],MovingCars[i]["Value"])-Plr.Character.PrimaryPart.Position).Magnitude
if Magnitude <= Range then
i.Parent = workspace.Vehicles
i.PrimaryPart.CFrame = CFrame.new(MovingCars[i]["PrimaryX"],MovingCars[i]["PrimaryY"],MovingCars[i]["Value"]) * CFrame.Angles(math.rad(MovingCars[i]["XAngle"]),math.rad(MovingCars[i]["YAngle"]),math.rad(MovingCars[i]["XAngle"]))
else
i.Parent = game.ReplicatedStorage.VehicleHolder
end
end
end
end
what’s your actual raw fps change compared to before, script activity isn’t too helpful for finding that out try using micro profiler to see each of the events, their names and their usage per frame. press ctrl+f6 then pause it with ctrl+p, way more usable information than script usage and rate.
I am not entirely sure how to interpret the microprofiler, so here’s a screenshot of a microprofiler dump. I also introduced a system where instead of deleting a car when it gets to the other end, it recycles it to stop the rapid cloning.
(sorry for my 2 week late response) that’s good, all your lag is from roblox’s side of stuff so any more optimization wont do anything.