How to make this smoother?

So i was following a tutorial about server side head moment and stuff, and he mainly replicated it to the clients, which made it a issue with getting the guns position and stuff, so i tried to modify make it more replicated over the server, it works but is very choppy

local script

local cam = workspace.Camera
local character = game.Players.LocalPlayer.Character
local root = character:WaitForChild("HumanoidRootPart")
local waist = character:FindFirstChild("Waist", true)
local yOffset = waist.C0.Y
local CfN, CfA, Asin = CFrame.new, CFrame.Angles, math.asin

game:GetService("RunService").RenderStepped:Connect(function()
	local cameraDirection = root.CFrame:toObjectSpace(cam.CFrame).lookVector
	if waist then 
		waist.C0 = CfN(0,yOffset,0) * CfA(0, -Asin(cameraDirection.X), 0) * CfA(Asin(cameraDirection.Y), 0, 0)
		if character:FindFirstChildOfClass("Tool") then
			local C = true
		end
	end
end)

while wait(0.1) do
	game.ReplicatedStorage.TotallyCoolEvents.Look:FireServer(waist.C0)
end

Server Script

game.ReplicatedStorage.TotallyCoolEvents.Look.OnServerEvent:Connect(function(player, nCF)
	local A = player.Name
	local C = game.Workspace:FindFirstChild(A)
	local D = C:FindFirstChild("Waist", true)
	D.C0 = nCF
end)

What it looks like
Great

1 Like

Maybe consider using TweenService to make it run smoother on the server side. TweenService

1 Like

Switch your local script to this:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Waist = Character:FindFirstChild("Waist", true)
local RemoteEvent = ReplicatedStorage.TotallyCoolEvents.Look
local cam = workspace.Camera

--//Controls
local yOffset = Waist.C0.Y
local CfN, CfA, Asin = CFrame.new, CFrame.Angles, math.asin

--//Functions
RunService.RenderStepped:Connect(function()
	local cameraDirection = HumanoidRootPart.CFrame:ToObjectSpace(cam.CFrame).LookVector

	if Waist then 
		Waist.C0 = CfN(0, yOffset, 0) * CfA(0, -Asin(cameraDirection.X), 0) * CfA(Asin(cameraDirection.Y), 0, 0)
	end
end)

while task.wait(0.01) do
	RemoteEvent:FireServer(Waist.C0)
end

And your server script to this:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local RemoteEvent = ReplicatedStorage.TotallyCoolEvents.Look

--//Functions
RemoteEvent.OnServerEvent:Connect(function(player, nCF)
	local character = Players:WaitForChild(player).Character
	local D = character:FindFirstChild("Waist", true)
	D.C0 = nCF
end)

I tried using this and it got really buggy, and was still pretty choppy
Greater

Also had to change this to

local character = game.workspace:FindFirstChild(player.Name)

Sorry I made an error.
Switch your local script with this:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Waist = Character:FindFirstChild("Waist", true)
local RemoteEvent = ReplicatedStorage.TotallyCoolEvents.Look
local cam = workspace.Camera

--//Controls
local yOffset = Waist.C0.Y
local CfN, CfA, Asin = CFrame.new, CFrame.Angles, math.asin

--//Functions
RunService.RenderStepped:Connect(function()
	local cameraDirection = HumanoidRootPart.CFrame:ToObjectSpace(cam.CFrame).LookVector

	if Waist then 
		Waist.C0 = CfN(0, yOffset, 0) * CfA(0, -Asin(cameraDirection.X), 0) * CfA(Asin(cameraDirection.Y), 0, 0)
		
		RemoteEvent:FireServer(Waist.C0)
	end
end)

And your server script with this:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local RemoteEvent = ReplicatedStorage.TotallyCoolEvents.Look

--//Functions
RemoteEvent.OnServerEvent:Connect(function(player, nCF)
	local character = player.Character
	local D = character:FindFirstChild("Waist", true)
	D.C0 = nCF
end)
1 Like

This did work!

Greatest

Although one more quick question, if i have alot of people like 20~ in a game at once, will the RemoteEvent Start breaking at any point?

The remote events won’t break but there might be a bit of lag on the server. If lag does occur you can fire the event every 5 frames so there isn’t much strain.

1 Like

Probably the last thing, could be a simple answer but way to tired rn, but when the player dies it spams the output with

1 Like

Oops forgot to account for that, switch your local script to this:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Waist = Character:FindFirstChild("Waist", true)
local RemoteEvent = ReplicatedStorage.TotallyCoolEvents.Look
local cam = workspace.Camera

--//Controls
local yOffset = Waist.C0.Y
local CfN, CfA, Asin = CFrame.new, CFrame.Angles, math.asin

--//Functions
RunService.RenderStepped:Connect(function()
	local cameraDirection = HumanoidRootPart.CFrame:ToObjectSpace(cam.CFrame).LookVector

	if not Waist then
		return
	end
	
	local C0 = Waist:FindFirstChild("C0")
	
	if not C0 then
		return
	end
	
	C0 = CfN(0, yOffset, 0) * CfA(0, -Asin(cameraDirection.X), 0) * CfA(Asin(cameraDirection.Y), 0, 0)

	RemoteEvent:FireServer(C0)
end)