How do I make this server-sided?

So uh…

someone sent me this local script to make the player’s character look at the mouse

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() ; Character = Player.Character
local Torso = Character:WaitForChild('Torso')
local Neck = Torso:WaitForChild("Neck")
local LeftShoulder = Torso:WaitForChild("Left Shoulder")
local RightShoulder = Torso:WaitForChild("Right Shoulder")
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.AutoRotate = false
local HMR = Character:WaitForChild("HumanoidRootPart")

local Mouse = Player:GetMouse()

local RC0 = CFrame.new(1, .5, 0, 0, 0, 1, 0, 1, 0, -1, 0, 0)
local RC1 = CFrame.new(-.5, .5, 0, 0, 0, 1, 0, 1, 0, -1, 0, 0)

local LC0 = CFrame.new(-1, .5, 0, 0, 0, -1, 0, 1, 0, 1, 0, 0)
local LC1 = CFrame.new(.5, .5, 0, 0, 0, -1, 0, 1, 0, 1, 0, 0)

local NeckC0 = Neck.C0
local NeckC1 = Neck.C1

game:GetService("RunService").RenderStepped:connect(function()
	local cf = workspace.CurrentCamera.CFrame.LookVector.Y

	local Kek = CFrame.Angles(0, 0, math.asin(Mouse.Origin.LookVector.Y))
	RightShoulder.C1 = RC1 * Kek:inverse()
	LeftShoulder.C1 = LC1 * Kek

	Neck.C0 = NeckC0 * CFrame.Angles(math.asin(Mouse.Origin.lookVector.Y), 0, 0):inverse()
	HMR.CFrame = CFrame.lookAt(HMR.Position, Vector3.new(Mouse.Hit.Position.X, HMR.Position.Y, Mouse.Hit.Position.Z), HMR.CFrame.UpVector)
end)

Here’s what it looks like on the client:

server:
image

can someone please help

That’s because It’s a local script, local scripts only execute in the client based on where It is located. Also, why do you need the player to look at the mouse in the server?

because its essential for my gun system so that people can see what other people are aiming at
RobloxScreenShot20220815_114119752

Alright so basically how I made these type of systems are:

  • Do the math calculations client side.
  • Send that information to the server via Remote Event.
  • Set everything on the server.

This is a general idea of how I get mine working, I would just DM you the rewritten code, but I’m not on pc at the moment sorry.

Already tried that, but the variables just don’t ever wanna line up with their names that were given on the client side

First step, create a “RemoteEvent” in “ReplicatedStorage” and name said event “BodyToMouseCFrameEvent.”

Client Script:

--/// Services:
local ReplicatedStorage = game:GetServices('ReplicatedStorage');
local PlayerService = game:GetService('Players');
local RunService = game:GetService('RunService');

--/// Constants:
local Camera = Workspace.CurrentCamera;

--// Player:
local LocalPlayer = PlayerService.LocalPlayer;
local Mouse = LocalPlayer:GetMouse();

--// Character:
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait();
local RootPart = Character:WaitForChild('HumanoidRootPart');
local Humanoid = Character:WaitForChild('Humanoid');
local Torso = Character:WaitForChild('Torso');
local Neck = Torso:WaitForChild('Neck');

local LeftShoulder = Torso:WaitForChild('Left Shoulder');
local RightShoulder = Torso:WaitForChild('Right Shoulder');

--// Instances:
local RemoteEvent = ReplicatedStorage:WaitForChild('BodyToMouseCFrameEvent');

--/// Variables:
local NeckC0, NeckC1 = Neck.C0, Neck.C1;

--/// Local Functions:
local BodyToMouseCFrame = function()
local MouseLookVector, HitPosition = Mouse.Origin.LookVector, Mouse.Hit.Position;
local Kek = CFrame.Angles(0, 0, math.asin(MouseLookVector.Y));

local RightPosition = RC1 * Kek:inverse();
local LeftPosition = LC1 * Kek:inverse();

return RemoteEvent:FireServer(MouseLookVector, HitPosition, RightPosition, LeftPosition);
end;

--/// Connections:
RunService.RenderStepped:Connect(BodyToMouseCFrame);

Server Script:

--/// Services:
local ReplicatedStorage = game:GetServices('ReplicatedStorage');

--/// Constants:
--// Instances:
local RemoteEvent = ReplicatedStorage:WaitForChild('BodyToMouseCFrameEvent');

--/// Local Functions:
local BodyToMouseCFrame = function(Player, LookVector, HitPosition, RightPosition, LeftPosition))
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait();
local RootPart = Character:WaitForChild('HumanoidRootPart');
local Humanoid = Character:WaitForChild('Humanoid');
local Torso = Character:WaitForChild('Torso');
local Neck = Torso:WaitForChild('Neck');

local LeftShoulder = Torso:WaitForChild('Left Shoulder');
local RightShoulder = Torso:WaitForChild('Right Shoulder');

Humanoid.AutoRotate = false;

RightShoulder.C1, LeftShoulder.C1 = RightPosition, LeftPosition;
Neck.C0 *= CFrame.Angles(math.asin(LookVector.Y), 0, 0):inverse();

RootPart.CFrame = CFrame.lookAt(RootPart.Position, Vector3.new(HitPosition.X, RootPart.Position.Y, HitPosition.Z), RootPart.CFrame.UpVector);
end;
2 Likes

Use this resource made by clonetrooper, it’s lightweight, works for all tools, shows hands and view model, basically everything if you want something realistic with tools, environment, etc.

Yes, tools follow with camera same with body, server and client side. Very performant friendly for what it does.

If you read the original code, it’s not setting the “body” in relation to the camera, it’s actually to the mouse position. That’s what my rewrite was for.

You will need to fire a remote event when you do changes in the client that are not replicated to the server, if you wish to do so

I already explained that, but they’ve had issues transitioning from client to server, so I just rewrote the code in the end for them.

2 Likes

image
slight issue

Yellow highlight doesn’t need to be bad, it’s just roblox suggesting something, have you tested the script first?

And what does “Find All” say that the highlight is for?

i need the neck.c0

doesnt work

Never read your code, was giving a resource that already did what post asked for and more.

I’m having problems with variables…

can someone please help me…

[character limit]

I tried to fix it, no more orange lines under it but now my character keeps rubberbanding up and down in the air, and when i dropped to the ground, i couldn’t walk.

--/// Services:
local ReplicatedStorage = game:GetService('ReplicatedStorage');

--/// Constants:
--// Instances:
local RemoteEvent = ReplicatedStorage:WaitForChild('BodyToMouseCFrameEvent');

--/// Local Functions:
local BodyToMouseCFrame = function(Player: Player, LookVector: Vector3, Position: Vector3, Kek: CFrame, RC0, RC1, LC0, LC1, NeckC0)
	local Character = Player.Character or Player.CharacterAdded:Wait();
	local RootPart = Character:WaitForChild('HumanoidRootPart');
	local Humanoid = Character:WaitForChild('Humanoid');
	local Torso = Character:WaitForChild('Torso');
	local Neck = Torso:WaitForChild('Neck');

	local LeftShoulder = Torso:WaitForChild('Left Shoulder');
	local RightShoulder = Torso:WaitForChild('Right Shoulder');
	

	RightShoulder.C1, LeftShoulder.C1 = RC1 * Kek:Inverse(), LC1 * Kek;
	Neck.C0 = NeckC0 * CFrame.Angles(math.asin(LookVector.Y), 0, 0):Inverse();

	RootPart.CFrame = CFrame.lookAt(RootPart.Position, Vector3.new(Position.X, RootPart.Position.Y, Position.Z), RootPart.CFrame.UpVector);
end;

RemoteEvent.OnServerEvent:Connect(BodyToMouseCFrame)

Technically works though…

can you actually answer me please