I've been having a problem with my script that the CFrame animations that it uses always tend to lag and it looks chopped up at times. I've seen other people's scripts where they look much smoother, but I don't know how to make it like that. I'm using RunService.Heartbeat and CFrame:Lerp for my animations, but I don't know if this is the best idea. I'm using the following code below.
local function MoveTabs(plr) local tabs = Core.Admins[plr.UserId].Data.Tabs local r Core.Services["RunService"].Heartbeat:Connect(function() local torso = plr.Character:FindFirstChild("HumanoidRootPart") if not torso then return else if #tabs ~= 0 then r = 3 + #tabs local x = 1 for i = 0, (360/#tabs)*#tabs, (360/#tabs) do if x > #tabs then x = 1 end local cf = CFrame.new(torso.Position) * CFrame.new(r * math.cos(math.rad(i)), 0, r * math.sin(math.rad(i))) local newCF = CFrame.new(cf.p, torso.Position) tabs[x].CFrame = tabs[x].CFrame:Lerp(newCF, .05) x = x + 1 end else r = 0 end end end) end
I would really appreciate if someone could explain to me a better way to accomplish this, as the bricks involved with this movement tend to look really laggy in larger servers (I'm running it in script builder). Help would be very appreciated, thank you.
[Edit]: I've heard of using TweenService to make smoother animations, but I don't know how that would work especially in a loop like this.
TweenService
is very easy to use and very smooth as well.
You also don't need a loop to use TweenService, you just create it, and play it, and it takes care of everything for you, which is part of why it's neat.
If you want to do an Animation
with TweenService
you can use RunService.Heartbeat
and just create a new tween with the new position, and play it every frame
.
Example:
local RunService = game:GetService("RunService") local TweenService = game:GetService("TweenService") local TweenInform = TweenInfo.new( 2, -- how long it takes to reach the goal Enum.EasingStyle.Back, -- EasingStyle Enum.EasingDirection.InOut, -- EasingDirection 1, -- how many times to repeat, if < 0 it will go forever false, -- If the tween reverses when repeating 2 -- how long it takes to do the animation again if it repeats ) RunService.Heartbeat:Connect(function() local cf = CFrame.new(torso.Position) * CFrame.new(r * math.cos(math.rad(i)), 0, r * math.sin(math.rad(i))) -- you're going to have to edit the 'i' here because I don't know what you're trying to do, you can make a variable and increment it and have a check to reset it, as this is similar to a loop tehnically local newCF = CFrame.new(cf.p, torso.Position) local goal = {CFrame = newCF} -- what you're tweening, and its value, in this case we are tweening the CFrame and it's value is newCF local tween = TweenService:Create(partToMove, TweenInform, goal) -- create the tween tween:Play() -- Play it end)
And you're done!
Hope I helped, if you have any questions please let me know!