Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you make the arm point towards where the player is looking?

So I made a script where if the user equips the tool their arm will point to where they are looking:

tool.Equipped:connect(function()
	debounce = true
	while debounce do
		
		wait()
		mouse = plr:GetMouse()
		rsh = tool.Parent:FindFirstChild("Torso")["Right Shoulder"]
		pos = Vector3.new(0,0,0)
		rsh.C1 = rsh.C1 * CFrame.new(pos,mouse.Origin.p)
	end
end)

tool.Unequipped:connect(function()
	debounce  = false
end)

But when I use the script the arm the joint is in the same position but is randomly pointing to random positions even when the cursor is still.

3 comments

0

Is this the full script?

0

no there is some other stuff that is irrelevent

0

try CFrame.Angles instead of CFrame.new

1 answer

This is only a partial answer.

The reason the arm is spinning wildly is that you aren't returning the shoulder to its original state each frame. To undo multiplying by a CFrame, you can multiply by its inverse.

Minor: debounce is the variable name traditionally used to prevent a function from running multiple times simultaneously. You're using it as a "continue to operate" variable (maybe just call it "equipped", "keepRunning/Executing", or just "run/execute"?)

Here's a partially fixed script:

local tool = script.Parent
local localPlayer = game.Players.LocalPlayer
local lastcf, rsh, run
tool.Equipped:connect(function(mouse)
	run = true
	lastcf = nil
	while run do
		wait(0.1)
		rsh = localPlayer.Character.Torso["Right Shoulder"]
		if lastcf then
			rsh.C1 = rsh.C1 * lastcf:inverse()
		end
		local cf = CFrame.new(Vector3.new(0, 0, 0), mouse.Origin.lookVector) * rsh.C1:inverse()
		rsh.C1 = rsh.C1 * cf
		lastcf = cf 
	end
	if lastcf then
		rsh.C1 = rsh.C1 * lastcf:inverse()
	end
end)

tool.Unequipped:connect(function()
	run = false
end)

Also, note that using the character's Torso won't work with the R15 character models.

Further, rsh = game.Players.LocalPlayer.Character.Torso["Right Shoulder"] is error prone -- if the Torso doesn't exist, the script will error. Using FindFirstChild("Torso") doesn't help if you then don't check if it returns nil! We can rewrite that bit like so:

		local torso = localPlayer.Character:FindFirstChild("Torso")
		if not torso then return end
		rsh = torso["Right Shoulder"]

The problem with the script is the line local cf = CFrame.new(Vector3.new(0, 0, 0), mouse.Origin.lookVector) * rsh.C1:inverse(). I know that you want the lookVector of mouse.Origin (the direction that the user is looking), not mouse.Origin.p (which is sort of the position of the camera), but I don't know what to do with it after that.

2 comments

0

I'm sorry I don't understand the purpose of :inverse()

0

Say 'cf' and 'original' are cframes. Then original * cf * cf:inverse() == original. In other words, you can divide by 'cf' by multiplying by cf:inverse() in the same way that 5 * 3 * (1/3) = 5, where 1/3 is the inverse of 3. The point of this is to restore rsh.C1 to what it was before (without removing any modifications the animation script did).

This discussion is preserved as it was. New answers, comments, and votes are closed.