Both arms following ball

What do you want to achieve?
A script that makes sure when the player clicks his mouse, that both of the character’s arms will follow any nearby ball. For only a few seconds

What is the issue?

  • tick() problem
  • both arms failed to follow any nearby ball

What solutions have you tried so far?

  • Searching “Arms following object”, although no luck.

Reference/Imagination on what I’m trying to make it look like:

My current code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local character = player.Character or player.CharacterAdded:Wait()
local humanoidrootpart = character:WaitForChild("HumanoidRootPart")
local LeftArm = character:WaitForChild("Left Arm", true)
local RightArm = character:WaitForChild("Right Arm", true)

local LeftArmC0 = character:WaitForChild("Torso")["Left Shoulder"].C0
local RightArmC0 = character:WaitForChild("Torso")["Right Shoulder"].C0

local BallsStorage = require(game.ReplicatedStorage.Modules.SettingsModule).BallStorage
local RunService = game:GetService("RunService")

local CatchDuration = 2
local CatchDebounce = false
local ClosestBall = nil
local TickTime

local PartList = {
	["WedgePart"] = true,
	["Part"] = true,
	["CornerWedgePart"] = true,
	["MeshPart"] = true,
	["TrussPart"] = true,
}

local function FollowBall()
if ClosestBall then else return false end
local relcamdirection = humanoidrootpart.CFrame.Rotation:PointToObjectSpace(ClosestBall.CFrame.LookVector)
	
if RightArm and LeftArm and CatchDebounce and character:WaitForChild("Humanoid").Health >0 then
	RightArmC0 = CFrame.new(RightArmC0.X, RightArmC0.Y, 0)
		* CFrame.Angles(0, -math.asin(relcamdirection.x), 0) 
		* CFrame.Angles(math.asin(relcamdirection.y), 0, 0)
		* CFrame.Angles(math.pi/2, 0, 0)
		
	LeftArmC0 = CFrame.new(LeftArmC0.X, LeftArmC0.Y, 0)
		* CFrame.Angles(0, -math.asin(relcamdirection.x), 0) 
		* CFrame.Angles(math.asin(relcamdirection.y), 0, 0)
		* CFrame.Angles(math.pi/2, 0, 0)
return true		
else		
return false		
end	
end	

local function LookForNearestBall()
	for i,v in pairs(workspace:FindFirstChild("BallsList"):GetChildren()) do
	if PartList[v.ClassName] and v.Name == require(game.ReplicatedStorage.Modules.SettingsModule).BallName then else return end	
	if ClosestBall == nil then
		ClosestBall = v
		return true	
	else
		if (character.PrimaryPart.Position - v.Position).magnitude < (ClosestBall.Position - character.PrimaryPart.Position).magnitude then
			ClosestBall = v
			return true	
		end
	end
	end
return false	
end

local function Reset()
print("resetted catching")		
ClosestBall = nil
TickTime = nil		
CatchDebounce = false	
RightArmC0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(0, -math.pi/-2, 0)
LeftArmC0 = CFrame.new(-1, 0.5, 0) * CFrame.Angles(0, -math.pi/2, 0)
RunService:UnbindFromRenderStep("FollowingBallBinding")	
end

local function AttemptCatch()
if LookForNearestBall() == true then
print("ball found near player")				
CatchDebounce = true
TickTime = tick()		
		
task.delay(CatchDuration*1.5,function()
CatchDebounce = false			
end)		
		
RunService:BindToRenderStep("FollowingBallBinding", -1,function()	
if FollowBall() == true then			
if (tick() - TickTime) <= CatchDuration then					
else
Reset()					
return					
end	
elseif (tick() - TickTime) > CatchDuration then					
print("catching duration reached")				
Reset()				
end		
end)			
		
else
print("failed to find a ball")		
CatchDebounce = false
Reset()		
return		
end	
end

mouse.Button1Down:Connect(function()
if not CatchDebounce and character:WaitForChild("Humanoid").Health >0 then		
print("called to attempt catching")		
AttemptCatch()	
end		
end)	
	
character:WaitForChild("Humanoid").Died:Connect(function()
Reset()	
end)

workspace.DescendantRemoving:Connect(function(Child)
	if PartList[Child.ClassName] then
		if Child == ClosestBall then 		
		Reset()									
		end			
	end		
end)

Help in any shape or form is appreciated!

1 Like

Roblox recently released the new IK constraints, you could maybe use that on the arms and set the end position to the ball so they “look” towards it. Seems like the easiest way to go about it if you don’t want to calculate all the fancy lookat vector/cframe math

3 Likes