Hello everyone. I am trying to simulate a subway train docking at a station. To do so, I move the entire model of the train (which is decently detailed) at a rate that constantly declines, as shown below.
local speed = 1.25
while speed > 0 do
script.Parent:TranslateBy(Vector3.new(0, 0, speed))
speed -= 0.001
game["Run Service"].Heartbeat:Wait()
end
The only problem is that while the code works correctly, my client’s FPS drops significantly while the train is moving. Is there a workaround, or is this just an insurmountable consequence of translating an object of such detail?
you should probably use delta time on slowing it down, also you should not use while loops, use game:GetService(“RunService”).Heartbeat:Connect, saves a instruction, cache the scripts parent, so you dont have to reget it, you should also probably do this on the server. maybe put the script in a actor too? (should improve performance if you use actors)
So then, would this be an appropriate use of deltatime? Additionally, I moved the script (and only the script) into an actor that I spawned into Workspace.
local speed = 1.25
while speed > 0 do
workspace.Train:TranslateBy(Vector3.new(0, 0, speed))
speed -= 0.001
local StartingTime = os.clock()
local DeltaTime1 = game["Run Service"].Heartbeat:Wait()
local DeltaTime2 = os.clock()-StartingTime
end
I have noticed the framerates improve somewhat, but the sequence is still noticeably laggy. Is there anything else that can be done? I did not understand what you meant by connecting the function to a heartbeat, as that would contradict using any wait functions at all.
No, that would not be a appropriate method of getting delta time, you should use game:GetService(“RunService”).Heartbeat:Connect(function(deltatime), delta time would be used to make sure it stays the same speed every time the train slows down.
local runService = game:GetService("RunService")
local speed = 1.25
local subway = script.Parent
local connection
local function heartbeat()
subway:TranslateBy(Vector3.new(0, 0, speed))
speed -= 0.001
if speed <= 0 then
connection:Disconnect()
end
end
connection = runService.Heartbeat:Connect(heartbeat)
I doubt it, the model is already loaded in, you’re just moving it.
I suppose you could try a tween, but you’ll need an approx. time to stop and end CFrame goal.
local tweenService = game:GetService("TweenService")
local subway = script.Parent
local tweenInfo = TweenInfo.new(
5, -- Approx. time to stop
Enum.EasingStyle.Quad,
Enum.EasingStyle.Out
)
local tween = tweenService:Create(
subway,
tweenInfo,
{WorldPivot = CFrame.new(0, 0, 0)} -- End goal CFrame
)
-- To play tween
tween:Play()
Edit 1: Changed easing style, you probably want to play around with the easing style to figure out which one is best for you