How to make arms follow mouse

So, I’m trying to make the arm follow the mouse, It’s functional but has a slight problem that I can’t figure out. I have edited this script to work with the right arm but the problem is when I go close to a part, the arm slightly goes up. I’m unsure how to resolve this problem, what seems the cause of the problem? And how do I fix it?
Here is the link of the script I used:

Thanks in advance!

5 Likes

You could set the mouse.TargetFilter to workspace to prevent the mouse hit from being on the nearby part. This way it will always be far away.

You can try this it moves within your camera:

 local Camera = workspace.Camera
 local char = game.Players.LocalPlayer.Character
 local HRP = char.HumanoidRootPart
 local rightShoulder = char.Torso["Right Shoulder"]
 local leftShoulder = char.Torso["Left Shoulder"]
 local asin = math.asin
 local YOS = rightShoulder.C0.Y
 locaL YOS1 = leftShoulder.C0.Y
 local CFNew, CFAng = CFrame.new, CFrame.Angles
 RunService.RenderStepped:Connect(function() 
local Direction = HRP.CFrame:toObjectSpace(Camera.CFrame).lookVector
    rightShoulder.C1 = CFNew(0, YOS, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(Direction.x)) * CFAng(-asin(Direction.y), 0, 0)
    leftShoulder.C1= CFNew(0, YOS1, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(Direction.x)) * CFAng(-asin(Direction.y), 0, 0)
  end)
10 Likes

Unknown global yos, There is no variable for it.

2 Likes

My bad forgot to define it I just updated the script

2 Likes

Thank you! It now doesn’t slightly go up. Thanks!

2 Likes

But how do I change the angle? Because it’s inverted to the other way

2 Likes

Glad you ask here is another script this one moves with your mouse:

local plr = game.Players.LocalPlayer
repeat wait(.1) until plr.Character
local character = plr.Character

local torso = character:WaitForChild("Torso")
local rightarm = character:WaitForChild("Right Arm")
local leftarm = character:WaitForChild("Left Arm")
local leftshoulder = torso:WaitForChild("Left Shoulder")
local rightshoulder = torso:WaitForChild("Right Shoulder")


local camera = game:GetService("Workspace").CurrentCamera


game:GetService("RunService").RenderStepped:Connect(function()

      if leftshoulder and leftarm then
	  leftshoulder.C0 = CFrame.new(-1, 0.5, -0.2) * CFrame.Angles(math.rad(70) + math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y), math.rad(-90), math.rad(52))
      end
      if rightshoulder and rightarm then
	   rightshoulder.C0 = CFrame.new(1, 0.5, -0.2) * CFrame.Angles(math.rad(120) + math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y), math.rad(90), math.rad(-104))
      end
end)
2 Likes

It’s still inverted for some reason.

1 Like

thats what i found weird i tried fixing but no luck But i have found something that can help you its a roblox file that has the arm move with mouse thing script here its the same script but this one is working way better :armMovement.rbxl (23.4 KB)

1 Like

Is there a way you could make that server-sided too so other players could see the arms movement?

1 Like

yea you can you basically get the position of the hand in the client then put that position of the hand it in the server then on the server you fire it to all the client which is a best approach here is the script

local script for arm:

local plr = game.Players.LocalPlayer
repeat wait(.1) until plr.Character
local character = plr.Character

local torso = character:WaitForChild("Torso")
local rightarm = character:WaitForChild("Right Arm")
local leftarm = character:WaitForChild("Left Arm")
local leftshoulder = torso:WaitForChild("Left Shoulder")
local rightshoulder = torso:WaitForChild("Right Shoulder")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UpgradePosEvent = ReplicatedStorage:WaitForChild("upgradepos") ---your remote event
local camera = game:GetService("Workspace").CurrentCamera

local Mouse = plr:GetMouse()


game:GetService("RunService").RenderStepped:Connect(function()

      if leftshoulder and leftarm then
	  leftshoulder.C0 = CFrame.new(-1, 0.5, -0.2) * CFrame.Angles(math.rad(70) + math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y), math.rad(-90), math.rad(52))
      end
      if rightshoulder and rightarm then
	   rightshoulder.C0 = CFrame.new(1, 0.5, -0.2) * CFrame.Angles(math.rad(120) + math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y), math.rad(90), math.rad(-104))
      end
end)

    while true do
    	if rightshoulder and rightarm then
    		UpgradePosEvent:FireServer(rightshoulder,rightshoulder.C0)
    	end
    	 if leftshoulder and leftarm then
    		UpgradePosEvent:FireServer(leftshoulder,leftshoulder.C0)
    	end
    	wait(.35)
    end

another local script put it in StarterPlayerscript:

 local plr = game.Players.LocalPlayer
UpgradePosEvent.OnClientEvent:Connect(function(thing, thing2, plaer)
	if thing and thing2 and plaer ~= plr then
		thing.C0 = thing2
	end
end)

Server script put in server script service:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local UpgradePosEvent = ReplicatedStorage:WaitForChild("upgradepos")

UpgradePosEvent.OnServerEvent:Connect(function(plr, thing, thingC0)

if plr and plr.Character and thing and thing.Parent and thing.Parent.Parent == plr.Character and thingC0 and thing:IsA("Motor6D") then

UpgradePosEvent:FireAllClients(thing, thingC0, plr)

end

end)
5 Likes

Thanks, it works perfectly now.

2 Likes

Sorry for asking but is possible to move the arms and head only when you have one tool equip?

something like this:

1 Like

well you can add something like this
that mean you add a variable then on the renderstep function you get to see if the variable is true then start changing position. i havnt been active on the script so i may forgot how i did the moving arm stuff

local toolEquip = false ---a variable to know if the tool is equiped
tool.Equipped:Connect(function()
toolEquip = true ---the tool is equiped so we make the variable to true
end
tool.Unequipped:Connect(function()
toolEquip = false ---the tool is Unequiped so we make the variable to false
end
game:GetService("RunService").RenderStepped:Connect(function()
      if toolEquip == false then return end  ----if the variable is false that mean the tool isnt equiped so we wont execute the script till the variable becomes true
      if leftshoulder and leftarm then
	  leftshoulder.C0 = CFrame.new(-1, 0.5, -0.2) * CFrame.Angles(math.rad(70) + math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y), math.rad(-90), math.rad(52))
      end
      if rightshoulder and rightarm then
	   rightshoulder.C0 = CFrame.new(1, 0.5, -0.2) * CFrame.Angles(math.rad(120) + math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y), math.rad(90), math.rad(-104))
      end
end)

    while true do
        if toolEquip == false then return end ---same here
    	if rightshoulder and rightarm then
    		UpgradePosEvent:FireServer(rightshoulder,rightshoulder.C0)
    	end
    	 if leftshoulder and leftarm then
    		UpgradePosEvent:FireServer(leftshoulder,leftshoulder.C0)
    	end
    	wait(.35)
    end

3 Likes

Oh thank you but I alr did something:


and works for every client

4 Likes

Would you mind to share how you did that? I’ve been looking on how to make it so that your arms follow your mouse only when you equip a tool for a while now.

1 Like

Yes ofc.

we move the arms on client and we replicated that moves on ServerSide and on Client again.

*Local script*

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer

local char = Player.Character
local origRightS = char:WaitForChild("Torso"):WaitForChild("Right Shoulder").C0
local origLeftS = char:WaitForChild("Torso"):WaitForChild("Left Shoulder").C0
local origNeck = char:WaitForChild("Torso"):WaitForChild("Neck").C0

local m = Player:GetMouse()

local UIS = game:GetService("UserInputService")

local IsEquipped = false

game:GetService("RunService").RenderStepped:Connect(function()
	if IsEquipped == true then

		if char.Torso:FindFirstChild("Right Shoulder") then
			char.Torso["Right Shoulder"].C0 = char.Torso["Right Shoulder"].C0:Lerp(CFrame.new(1, .65, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y), 1.55, 0, 0) , 0.1)
		end
		if char.Torso:FindFirstChild("Left Shoulder") then
			char.Torso["Left Shoulder"].C0 = char.Torso["Left Shoulder"].C0:Lerp(CFrame.new(-1, .65, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y), -1.55, 0, 0) , 0.1)
		end
		if char.Torso:FindFirstChild("Neck") then
			char.Torso["Neck"].C0 = char.Torso["Neck"].C0:Lerp(CFrame.new(0, 1, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y) + 1.55, 3.15, 0), 0.2)
		end

	else

		if char.Torso:FindFirstChild("Right Shoulder") then
			char.Torso["Right Shoulder"].C0 = char.Torso["Right Shoulder"].C0:lerp(origRightS, 0.1)
		end

		if char.Torso:FindFirstChild("Left Shoulder") then
			char.Torso["Left Shoulder"].C0 = char.Torso["Left Shoulder"].C0:lerp(origLeftS, 0.1)
		end

		if char.Torso:FindFirstChild("Neck") then
			char.Torso["Neck"].C0 = char.Torso["Neck"].C0:lerp(origNeck, 0.1)

		end
	end
end)

char.ChildAdded:Connect(function()
	for i,v in pairs(char:GetChildren()) do
		if v:IsA("Tool") then
			if v:FindFirstChild("HoldArmsStill") then

			else
				IsEquipped = true

			end
		end
	end
end)

game.ReplicatedStorage.Look.OnClientEvent:Connect(function(PlrAgain, neckCFrame, RsCFrame, LsCFrame)
	local Neck = PlrAgain.Character.Torso:FindFirstChild("Neck", true)
	local Rs = PlrAgain.Character.Torso:FindFirstChild("Right Shoulder", true)
	local Ls = PlrAgain.Character.Torso:FindFirstChild("Left Shoulder", true)
	
	if Neck then
		Neck.C0 = neckCFrame
	end
	
	if Rs then
		Rs.C0 = RsCFrame
	end
	
	if Ls then
		Ls.C0 = LsCFrame
	end
end)

while wait(2) do -- you can change here if you want
	game.ReplicatedStorage.Look:FireServer(char.Torso["Neck"].C0, char.Torso["Right Shoulder"].C0, char.Torso["Left Shoulder"].C0)
end


*ServerSide*

game.ReplicatedStorage.Look.OnServerEvent:Connect(function(player, neckCFrame, RsCFrame, LsCFrame)
	for key, value in pairs(game.Players:GetChildren()) do
		if value ~= player then
			game.ReplicatedStorage.Look:FireClient(value, player, neckCFrame, RsCFrame, LsCFrame)
		end
	end
end)

Note: RemoteEvent name is “Look”

5 Likes

What is “(“HoldArmsStill”)” and how is it added

1 Like

Where to put this? In serverscriptservices or workspace?

2 Likes