How to make a server side script that Rotates the head to the Direction that the camera is looking at?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

a server side script that Rotates the head to the Direction that the camera is looking at

  1. What is the issue? Include screenshots / videos if possible!

I tried to do it but it was laggy because I used remote events

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Search And Tried to user Remote Events but It was laggy

I don’t speak English very good so I hope you know what I want to achieve

1 Like

Alright. I will be very clear. The exact thing you mentioned is impossible.

You cannot have a server-side script managing the camera from the client. I did make a model called HeadCam and it is okay, but this is not what we’re looking for.

So, I recommend making a LocalScript that fires a RemoteEvent every half second, which will make the head movements very nice and clean on the client but slow and clunky on the server. This doesn’t matter too much if it’s slow on the server, because we want the server to be fast at doing other useful things like managing physics.

No need to rotate players head towards a camera through a script dependent on the server. This will create run time issues and will unnecessarily waste memory. Best bet is to rotate players head on the client. You can use run service for this. Particularly the RenderStepped function that is built into the API. Update players head or whatever your goal is for this thread within the render stepped function. Use Roblox developer page as a reference for implementation on RunService.

1 Like

Oh okay
Thanks for helping me guys!

This is pretty easy to do on client, but not so on server as well.

But here’s two scripts that does this for both client and server.

This first LocalScript should be placed in StarterCharacterScripts.

local tweenService = game:GetService("TweenService")

local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer

local Character = Player.Character
local Root = Character:WaitForChild("HumanoidRootPart")
local Neck = Character:FindFirstChild("Neck", true)
local YOffset = Neck.C0.Y

local CFNew, CFAng = CFrame.new, CFrame.Angles
local asin = math.asin

game:GetService("RunService").RenderStepped:Connect(function()
	local CameraDirection = Root.CFrame:toObjectSpace(Camera.CFrame).lookVector
	if Neck then
		if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
			Neck.C0 = CFNew(0, YOffset, 0) * CFAng(0, -asin(CameraDirection.x), 0) * CFAng(asin(CameraDirection.y), 0, 0)
		elseif Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
			Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
		end
	end
end)

game.ReplicatedStorage.Look.OnClientEvent:Connect(function(otherPlayer, neckCFrame)
	local Neck = otherPlayer.Character:FindFirstChild("Neck", true)

	if Neck then
		tweenService:Create(Neck, TweenInfo.new(.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0), {C0 = neckCFrame}):Play()
	end
end)

while wait(0.0001) do
	game.ReplicatedStorage.Look:FireServer(Neck.C0)
end

This next Script should be placed in ServerScriptService.

game.ReplicatedStorage.Look.OnServerEvent:Connect(function(player, neckCFrame)
	for key, value in pairs(game.Players:GetChildren()) do
		if value ~= player and (value.Character.Head.Position - player.Character.Head.Position).Magnitude < 10 then
			game.ReplicatedStorage.Look:FireClient(value, player, neckCFrame)
		end
	end
end)

And finally place a RemoteEvent that is named “Look” in ReplicatedStorage

Hopes this helps

2 Likes

Thanks for helping me But I already tried to use remote events and it was laggy
and I think it’s impossible to not use remote events

For some reason proximity prompts become invisible whenever I do this, Is there a fix?

They shouldn’t. Is RequiresLineOfSight disable?

I’m good now, I had an invisible gui over the ui, had nothing to do with it. Mustve added the gui when I added this. Sorry!