Hey! How are you doing? I was looking at the script given by @CleverSource and I noticed your reply. I also needed to make the event server-sided and after playing around a bit, I got the way to make the script server-sided. So, let me explain you how to make the script server-sided.
What you'll have to do:
- Make two RemoteEvents via the server-script. - One for getting the client's player and the other for getting the client-sided function. Ensure that the Parents and Names of the RemoteEvents are correctly declared.
- Declare the constant data in the server-script. - Create empty variables for the purpose.
- Go to the LocalScript and WaitFor the RemoteEvents.
- Declare the client data on the local script. - This is the data that the server-script cannot handle.
Fire the client's player-yielding RemoteEvent. - The alternate way to do this is to use
game:GetService("Players").PlayerAdded:Connect(function(player) --[[The content of the script]]-- end)
- Assign the values of the constant data declared above.
- Declare the variable values inside the client-sided function script. - This is done because this data is the main doer of the function.
- Fire the client-sided function script. - Here, I've used
game:GetService("RunService").RenderStepped:(function() --[[Firing the Server]]-- end)
- RunService cannot be fired on server-script.
- Call the function shown by @CleverSource inside the OnServerEvent connection.
And then you have it! you have successfully server-sided the script. For reference, I’ll add in the LocalScript and the Script.
Note: Some of the variables and constants have been removed from @CleverSource’s code, as that data was unused.
Local Script:
---------------------------------------------------------------------
local GetLocalData = game:GetService("ReplicatedStorage"):WaitForChild("GetLocalData")
local GetMouseFollowFunction = game:GetService("ReplicatedStorage"):WaitForChild("GetMouseFollowFunction")
--(iii)
---------------------------------------------------------------------
local CameraSubject = workspace.CurrentCamera.CameraSubject -- (iv)
GetLocalData:FireServer()-- (v)
RunService.RenderStepped:Connect(function()
local PlayerMouseHit = game.Players.LocalPlayer:GetMouse().Hit.p -- (vii)
GetMouseFollowFunction:FireServer(CameraSubject,PlayerMouseHit) -- (viii)
end)
Note: This is to be put in StarterCharacterScripts.
Server Script:
local GetLocalData = Instance.new("RemoteEvent")
local GetMouseFollowFunction = Instance.new("RemoteEvent")
GetLocalData.Name = "GetLocalData"
GetLocalData.Parent = game:GetService("ReplicatedStorage")
GetMouseFollowFunction.Name = "GetMouseFollowFunction"
GetMouseFollowFunction.Parent = game:GetService("ReplicatedStorage")
-- (i)
---------------------------------------------------------------------
local Character
local Head
local Neck
local Torso
local Waist
local WaistOriginC0
local NeckOriginC0
-- (ii)
---------------------------------------------------------------------
GetLocalData.OnServerEvent:Connect(function(Player)
Character = Player.Character or Player.CharacterAdded:Wait()
Head = Character:WaitForChild("Head")
Neck = Head:WaitForChild("Neck")
Torso = Character:WaitForChild("UpperTorso")
Waist = Torso:WaitForChild("Waist")
WaistOriginC0 = Waist.C0
NeckOriginC0 = Neck.C0
Neck.MaxVelocity = 1/3
end)
-- (vi)
---------------------------------------------------------------------
GetMouseFollowFunction.OnServerEvent:Connect(function(_,CameraSubject,PlayerMouseHit)
if Character:FindFirstChild("UpperTorso") and Character:FindFirstChild("Head") then
local TorsoLookVector = Torso.CFrame.lookVector
local HeadPosition = Head.CFrame.p
if Neck and Waist then
if CameraSubject:IsDescendantOf(Character) or CameraSubject:IsDescendantOf(Player) then
local Point = PlayerMouseHit
local Distance = (Head.CFrame.p - Point).magnitude
local Difference = Head.CFrame.Y - Point.Y
Neck.C0 = Neck.C0:lerp(NeckOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1, 0), 0.5 / 2)
Waist.C0 = Waist.C0:lerp(WaistOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5, 0), 0.5 / 2)
end
end
end
end)
-- (ix)
---------------------------------------------------------------------
Thanks for reading. If this helps, kindly leave a like.