How to make this head rotation work?

hi guys i was trying to do a skinned mesh character move his head with camera.cframe but what did i do wrong in the script?

server script

local RS = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function()
	RS.HeadRotation.OnServerEvent:Connect(function(plr, CameraCFrame)
		for i,otherplr in pairs(game.Players:GetChildren()) do
			if otherplr ~= plr then
				RS.HeadRotation:FireAllClients(otherplr,plr, CameraCFrame)
			end
		end
	end)
end)

localscript

RunService.RenderStepped:Connect(function()
	wait(0.5)
	game.ReplicatedStorage.HeadRotation:FireServer(Camera.CFrame)
end)

game.ReplicatedStorage.HeadRotation.OnClientEvent:Connect(function(otherplr,headcframe)
	local head = otherplr.Character.RootPart.Hips.Spine.Spine1.Spine2.Neck.Head
	
	if head then
		print(otherplr)
		TweenService:Create(head,TweenInfo.new(0.5, Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{TransformedWorldCFrame = headcframe}):Play()
	end
	
end)

it says its read only how can i edit the transformedworldcframe of a bone?

2 Likes

TransformedWorldCFrame is read-only. You cannot set it.
Bones are only moved through .Transform.

Fix: serverscript: local RS = game.ReplicatedStorage
local evt = RS.HeadRotation

game.Players.PlayerAdded:Connect(function(plr)
evt.OnServerEvent:Connect(function(sender, cf)
for _, other in ipairs(game.Players:GetPlayers()) do
if other ~= sender then
evt:FireClient(other, sender, cf)
end
end
end)
end)

localscript: local RS = game.ReplicatedStorage
local evt = RS.HeadRotation
local cam = workspace.CurrentCamera
local RunService = game:GetService(“RunService”)

RunService.RenderStepped:Connect(function()
evt:FireServer(cam.CFrame)
end)

evt.OnClientEvent:Connect(function(otherplr, cf)
local head = otherplr.Character.RootPart.Hips.Spine.Spine1.Spine2.Neck.Head
if head then
head.Transform = head.Transform:Lerp(cf - cf.Position, 0.2)
end
end)

it says position is not a member of players.player2

you referenced .Position on the Player instead of the character’s part.
Use otherplr.Character before using the hierarchy.

how can i do it sorry im a newbie

sure just replace the line with this
local char = otherplr.Character
local head = char.RootPart.Hips.Spine.Spine1.Spine2.Neck.Head

yo bro i tried to use transformedcframe cuz its animated and when i change the position its like a milisecon until it goes back to normal

It’s getting overwritten by any animations if you only apply it once. You need to apply it per-frame or use the CFrame property.

i apply it per 2 seconds do i need to make it lower like 0.5?

Cache the direction you get from the HeadRotation.OnClientEvent and apply it every RunService.PreRender or some other Runservice event.

like this?

game.ReplicatedStorage.HeadRotation.OnClientEvent:Connect(function(otherplr,headcframe)
	RunService.PreRender:Connect(function()
		local char = otherplr.Character
		local head = char.RootPart.Hips.Spine.Spine1.Spine2.Neck.Head
		
		if head then
			head.Transform = head.TransformedCFrame:Lerp(headcframe - headcframe.Position, 0.2)
		end
	end)
end)

it just crashed my pc… do i need add wait to it?

your connecting a new runservice.prerender everytime the event fires thats why your pc crashed

That should work but that is going to create a new connection everytime the HeadRotation event fires ( aka a memory leak )

Instead whenever you recieve the headcframe from the HeadRotation event cache it in a dictionary with the key being the other player.

local cache = {}

--# OnClient Event
    cache[otherplr] = headcframe

Then create a simple connection to the RunService.PreRender and loop through the cache and apply the caches direction.

--# RunService.PreRender Event
    for player, cframe in pairs(cache) do
        --# Get the head weld/bone/motor6d whatever
        --# Apply the cframe
    end

Also remember to connect an event to Players.PlayerRemoving to clean up the cache of leaving players to prevent a memory leak.

--# Game.Players.PlayerRemoving Event
    if cache[player] then
        cache[player] = nil
    end

it doesnt rotate but it just makes the head kindly rotated to the front

local cache = {}

--# OnClient Event

RunService.RenderStepped:Connect(function()
	game.ReplicatedStorage.HeadRotation:FireServer(Camera.CFrame)
	wait(0.5)
end)	

game.ReplicatedStorage.HeadRotation.OnClientEvent:Connect(function(otherplr,headcframe)
		cache[otherplr] = headcframe
	RunService.PreRender:Connect(function()
		for player, cframe in pairs(cache) do
			local char = otherplr.Character
			local head = char.RootPart.Hips.Spine.Spine1.Spine2.Neck.Head
		
			if head then
				head.Transform = head.Transform:Lerp(headcframe - headcframe.Position, 0.2)
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function()
	if cache[Player] then
		cache[Player] = nil
	end
end)

You should move the RunService connection out of the HeadRotation event. Also waiting in a connection doesn’t prevent it from firing for the wait duration so you should make it a loop for now.

For example:

task.spawn(function()
    while task.wait(0.5) do
        --# :FireServer here.
    end
end

Is there a way you could show me a video or photo of how it currently looks?

its getting laggier until its crashes

local cache = {}

--# OnClient Event
task.spawn(function()
	while task.wait(0.5) do
		RunService.RenderStepped:Connect(function()
			game.ReplicatedStorage.HeadRotation:FireServer(Camera.CFrame)
		end)
	end
end)
	

game.ReplicatedStorage.HeadRotation.OnClientEvent:Connect(function(otherplr,headcframe)
	cache[otherplr] = headcframe
	RunService.PreRender:Connect(function()
		for player, cframe in pairs(cache) do
			local char = otherplr.Character
			local head = char.RootPart.Hips.Spine.Spine1.Spine2.Neck.Head
		
			if head then
				head.Transform = head.Transform:Lerp(headcframe - headcframe.Position, 0.2)
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function()
	if cache[Player] then
		cache[Player] = nil
	end
end)

local cache = {}

--# OnClient Event
task.spawn(function()
	while task.wait(0.5) do
		game.ReplicatedStorage.HeadRotation:FireServer(Camera.CFrame)
	end
end)
	

game.ReplicatedStorage.HeadRotation.OnClientEvent:Connect(function(otherplr,headcframe)
	cache[otherplr] = headcframe
end)

RunService.PreRender:Connect(function()
		for player, cframe in pairs(cache) do
			local char = otherplr.Character
			local head = char.RootPart.Hips.Spine.Spine1.Spine2.Neck.Head
		
			if head then
				head.Transform = head.Transform:Lerp(headcframe - headcframe.Position, 0.2)
			end
		end
	end)

game.Players.PlayerRemoving:Connect(function()
	if cache[Player] then
		cache[Player] = nil
	end
end)

This is what I mean. Should have probably worded it different.

it cant detech the otherplr and headcframe should i make a local to them to make it work?

Ah. In the RunService.PreRender the player variable is called player and the cframe variable is called cframe. Not otherplr and headcframe. You can change those.