I’ve been working on a script where Arms, torso, and the characters head follows the mouse
The issue I am experiencing is that the script i have already moves the whole character.
local plr = game:GetService("Players").LocalPlayer
game:GetService("RunService").RenderStepped:connect(function()
if plr.Character then
local root = plr.Character.HumanoidRootPart
if root then
root.CFrame = CFrame.new(root.CFrame.p,root.CFrame.p+Vector3.new(cam.CFrame.lookVector.X,0,cam.CFrame.lookVector.Z))
end
end
end)
i’ve tried messing with humanoid parts and what exactly it needs to move, but so far hasn’t done anything for me.
Like when you move your mouse up and down the character doesn’t move but what i want it to do is the torso and head to go up and down with the mouse, does that help?
I have made a simple local script for you which makes the player’s head and torso follow the player’s mouse. I’ll have a working example below, the .rbxl file provided, and the code! I hope this helps. If you have any questions feel free to ask me.
Code:
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local PlayerMouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local Character = Player.Character or Player.CharacterAdded:Wait()
local Head = Character:WaitForChild("Head")
local Neck = Head:WaitForChild("Neck")
local Torso = Character:WaitForChild("UpperTorso")
local Waist = Torso:WaitForChild("Waist")
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local NeckOriginC0 = Neck.C0
local WaistOriginC0 = Waist.C0
Neck.MaxVelocity = 1/3
RunService.RenderStepped:Connect(function()
local CameraCFrame = Camera.CoordinateFrame
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 Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then
local Point = PlayerMouse.Hit.p
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)
Can you please, in depth, explain how the code works? I know some basics of coding, but I don’t know globals (such as workspace) in your code, or what the Neck and Waist C0 lerp is doing. Since there are no YouTube tutorials on this topic (that I know of), and I need to ask you so I can understand how to make the character move that smoothly.
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.
local GetLocalData = Instance.new("RemoteEvent")
local GetMouseFollowFunction = Instance.new("RemoteEvent")
GetLocalData.Name = "GetLocalData"
GetLocalData.Parent = game:GetService("ReplicatedStorage"):WaitForChild("General")
GetMouseFollowFunction.Name = "GetMouseFollowFunction"
GetMouseFollowFunction.Parent = game:GetService("ReplicatedStorage"):WaitForChild("General")
-- (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)
---------------------------------------------------------------------