I’m very new to CFrame and Tweening, but I am trying to move a roller coaster car around the track using TweenService. It Tweens the car between each numbered node. I tried running the Tween on the server(Script in workspace), but it lags in a live server.
I then tried to run the Tween on the client(LocalScript in ReplicatedFirst). The car starts to jitter constantly (not lag every so often) a lot. I’ve done hours of looking for solutions, but have not found that anyone has had the same issue.
wait(5)
local WaitValue = script.WaitTime --NumberValue inside the script
local TrackCount = 0
--Name all nodes
for i, v in pairs(game.Workspace["Compiled New Roller Coaster"]:GetChildren()) do
TrackCount = TrackCount + 1
v.Name = tostring(TrackCount)
end
local TweenService = game:GetService("TweenService")
local Train = game.Workspace.Train
wait(1)
local CurrentTrack1 = 0
while true do
CurrentTrack1 = CurrentTrack1 + 1
local Info = TweenInfo.new(
WaitValue.Value,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
0,
false,
0
)
local Goals = {CFrame = game.Workspace["Compiled New Roller Coaster"][CurrentTrack1].CFrame}
local Tween1 = TweenService:Create(Train, Info, Goals)
Tween1:Play()
if CurrentTrack1 == TrackCount then
CurrentTrack1 = 0
end
Tween1.Completed:Wait()
end
If you guys need any more info, just let me know and I’ll be happy to provide.
Edit: The WaitValue is set at .9
Edit2: I thought the jittering only happened on the client when I was sitting in the seat, but I looked closer and it happens even when I’m not sitting in the seat.
Edit: Maybe not. I think that’s focused more for normal Roblox physics stuff. I think maybe using remotes to communicate to all clients the settings for the tween and running them there?..
Uh, I don’t think I’m giving sound advice here. You can maybe look at some of this stuff, but I don’t know if it’s a solution or really anything helpful. Sorry! Kind of tired, probably gonna call it a night now.
After a fix has been made to the jittering, I think I am going to have to use remotes to make sure that the car is synced for everyone in the game some time in the future. Thank you!
Anchor the car. If you anchor the object, network replication for physics is simply disabled. It will then be smooth for clients. If you want the car to not be anchored and utilize Roblox physics, do not use CFrame in order to move your car.
I sadly can’t say that it is as easy as checking the “anchored” box . The car is anchored and the seat is welded to the car using a WeldConstraint. I thought that maybe the Weld could be the issue, so I Anchored the seat as well and tweened both at the same time. Same jittery result as soon as I sat in the seat sadly. Thank you for the reply!
You can group the entire car assembly as a single model. Anchor all parts in this model. Then, utilize SetPrimaryPartCFrame to move your model. This will translate the primary part of the model, and all additional parts in the model will move relative as well.
I decided to see what would happen if I try to make a bare bones Tween on the client. Even after that it would not tween smoothly for some reason. If you spot any errors with my code, please let me know. Thank you!
local TweenService = game:GetService("TweenService")
local Car = game.Workspace:WaitForChild("Car")
local Target = game.Workspace:WaitForChild("Target")
local TSInfo = TweenInfo.new(
2.5,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
0,
false,
0
)
local Goal = {CFrame = Target.CFrame}
local Tween = TweenService:Create(Car, TSInfo, Goal)
-- wait for character to load in to see tween
wait(6)
Tween:Play()
I ended up using :Lerp() instead of TweenService to get the desired results. Thank you all for your time and help!
Here is my code (this includes syncing for all clients):
Server-Side
local RS = game:GetService("ReplicatedStorage")
local RE = RS:WaitForChild("TestingCoaster")
local Train = game.Workspace:WaitForChild("Train")
local Speed = script.Speed
local CT1 = 1
local CT2 = 0
local C1SP = Train.Car1.CFrame
local C2SP = Train.Car2.CFrame
wait(7)
while true do
CT1 = CT1 + 1
CT2 = CT2 + 1
for i = 0,1,.3 do
RE:FireAllClients(Train, C1SP, C2SP, CT1, CT2, i)
wait()
end
C1SP = game.Workspace["Compiled New Roller Coaster"][CT1].CFrame
C2SP = game.Workspace["Compiled New Roller Coaster"][CT2].CFrame
if CT1 == 190 then
CT1 = 1
CT2 = 0
end
game:GetService("RunService").Stepped:Wait()
end
Client-Side
local RS = game:GetService("ReplicatedStorage")
local RE = RS:WaitForChild("TestingCoaster")
local NodesModel = game.Workspace:WaitForChild("Compiled New Roller Coaster")
RE.OnClientEvent:Connect(function(Train, C1SP, C2SP, CT1, CT2, i)
Train.Car1.CFrame = C1SP:Lerp(NodesModel[CT1].CFrame, i)
Train.Car2.CFrame = C2SP:Lerp(NodesModel[CT2].CFrame, i)
end)
I think I need to use tables to clean up my event parameters at some point.