Heyo,
I have a script which I will put below that basically gives the player a tilt/lean whenever they go left or right, the script I used was designed for the client end and worked perfectly however when converting it to a normal script (because I want the tilt to be seen by others) it doesn’t go as smoothly as it did before and my concern is that eventually if a lot of people are in the game it will cause a lot of lag.
wait()
local Debris = game:GetService("Debris")
local Run = game:GetService('RunService')
local UIS = game:GetService("UserInputService")
local Tween = game:GetService("TweenService")
local Players = game:GetService("Players")
local char = script.Parent
local player = Players:GetPlayerFromCharacter(char)
local human = char.Humanoid
local root = char.HumanoidRootPart
local rootJoint = root:WaitForChild("RootJoint")
local maxDegrees = 15
local turnAmount = math.rad(root.Velocity.X)
local dir, vel, dir2, vel2
local angle = 0
local angle2 = 0
local originalC0 = rootJoint.C0
Run.Heartbeat:Connect(function()
vel = root.Velocity * Vector3.new(1,0,1)
vel2 = root.Velocity * Vector3.new(1,0,1)
if vel.Magnitude > 2 then
dir = vel.Unit
angle = root.CFrame.RightVector:Dot(dir)
else
angle = 0
end
if vel2.Magnitude > 2 then
dir2 = vel.Unit
angle2 = root.CFrame.lookVector:Dot(dir2)
else
angle2 = 0
end
rootJoint.C0 = rootJoint.C0:Lerp(originalC0 * CFrame.Angles(((angle2 * 0) ), ((angle * -1) / 3), 0), 0.1)
end)
Any help in trying to make this optimised for server side or any alternative scripting I could use to remove the potential server lag and increase smoothness would be appreciated.
Currently the script is in StarterCharacterScripts and is a server script
(usually I have it as local but yk tryna make it not just on the client)
Maybe instead of RunService you could detect a change in the character’s HumanoidRootPart’s MoveDirection (so it doesn’t constantly run but only when the player moves)
Define the connection like so: local connection = Run.Heartbeat:Connect(function()
Add the following to stop the connection when the player stops moving around:
local humanoid = char:WaitForChild("Humanoid")
humanoid.StateChanged:Connect(function()
if humanoid.MoveDirection.Magnitude == 0 then
connection:Disconnect()
end
end)
There is probably a better way to do it, but I’d start by making it stop whenever the characters stop moving around.
Never use depricated functions like wait(). Settle for task.wait() instead.
You’re sacrificing so much performance when on a wide scale.
Also putting wait() at the top is the most useless thing here. Scripts go top down and wont loop. Unless ofc you are using a while, repeat or something else that loops a thread.
Speaking of loops. Use corutines and task.spawn() for example.
Both Corutines and Task are there to help optimise your code.
Use parralel lua when possible, performance is so much faster when using actors.
For debugging I recommend you add a print at the bottom of the heatbeat under the rootJoint.C0 and put something like this: print("Time between exec:" ..step).
You will never be able to. The server’s heartbeat and client’s render processes are not synced, and physically can not be. Therefore, anything ran on the client will always appear smoother.
My advice is to keep this on the client. Show the client an initial response to their input right away to keep the system feeling fast and responsive. Then fire the update through the server, and to all of the other clients so they can also see it. This is a common practice in doing these “manual” CFrame animations.
Not on the server, no. The only way to make smooth animations on the server is by using actual animations. In that case it still really is client work, roblox just handles it for you.
Hello, there is indeed a way to make the animations “smoother”. To do this, you’ll need to open the server script and broadcast the player lean information (i.e everything you’d have needed to render it client-side such as the tilt angle). Since we can’t broadcast this information to other players in real time, we’ll use tweens to fill in the gaps on their side.
Here is what it looks like in a head rotation system. Despite only updating the position every 0.5 seconds (this is done to simulate a poor internet connection), the animation is fairly smooth thanks to a sine tween.