Feedback on Script that moves the player's hands and head based on mouse position

Hello! I’m developing a Medieval Fighting game not unlike Blood & Iron and I’m looking for feedback on a system I developed to make the player’s hands look at the mouse on both the client and server.
I have a local script placed in StartedPlayerScripts, an UnrealiableEvent in ReplicatedStorage, and a script in StarterPlayerScripts.

The Local script:

local lookAtMouseEvent = game.ReplicatedStorage:WaitForChild("LookAtMouseEvent")

local head = char:FindFirstChild("Head")
local headCFrame = head.CFrame

local rightArm = char:FindFirstChild("Right Arm")
local rightArmCFrame = rightArm.CFrame

local leftArm = char:FindFirstChild("Left Arm")
local leftArmCFrame = leftArm.CFrame


local torso = char:WaitForChild("Torso")
local neck = torso:WaitForChild("Neck")
local leftShoulder = torso:WaitForChild("Left Shoulder")
local rightShoulder = torso:WaitForChild("Right Shoulder")

local neckOriginC0 = neck.C0
local leftOriginC0 = leftShoulder.C0
local rightOriginC0 = rightShoulder.C0

mouse.Move:Connect(function()
	
	if char:FindFirstChild("Torso") and char:FindFirstChild("Head") then
		local TorsoLookVector = torso.CFrame.lookVector
		
		local headPosition = head.CFrame.p
		local leftArmPosition = leftArm.CFrame.p
		local rightArmPosition = rightArm.CFrame.p
		local mousePos = mouse.Hit.p
		
		local Distance = (head.CFrame.p - mousePos).magnitude
		local Difference = head.CFrame.Y - mousePos.Y
		
		
		local goalNeckCFrame = CFrame.Angles(-(math.atan(Difference / Distance)), 0, 0)
		local neckLerp = neck.C0:lerp(goalNeckCFrame*neckOriginC0, 0.8).Rotation + neckOriginC0.Position
		neck.C0 = neckLerp
		
		local goalLeftCFrame = CFrame.Angles(-(math.atan(Difference / Distance)), 0, 0)
		local leftLerp = leftShoulder.C0:lerp(goalLeftCFrame*leftOriginC0, 0.8).Rotation + leftOriginC0.Position
		leftShoulder.C0 = leftLerp
		
		local goalRightCFrame = CFrame.Angles(-(math.atan(Difference / Distance)), 0, 0)
		local rightLerp = rightShoulder.C0:lerp(goalRightCFrame*rightOriginC0, 0.8).Rotation + rightOriginC0.Position
		rightShoulder.C0 = rightLerp
		
		lookAtMouseEvent:FireServer(neckLerp, leftLerp, rightLerp) --fires Event in ReplicatedStorage, LookAtMouseServer in StarterCharacter scripts uses this event
	end
		
end)

The script:

game.ReplicatedStorage.LookAtMouseEvent.OnServerEvent:Connect(function(player, headFrame, leftFrame, rightFrame)
	
	local name = player.Name
	local char = game.Workspace:FindFirstChild(name)
	
	
	local torso = char:FindFirstChild("Torso")
	local neck = torso:FindFirstChild("Neck")
	local leftShoulder = torso:FindFirstChild("Left Shoulder")
	local rightShoulder = torso:FindFirstChild("Right Shoulder")
	
	neck.C0 = headFrame
	leftShoulder.C0 = leftFrame
	rightShoulder.C0 = rightFrame
	
end)

image

1 Like