local v=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local d=game:GetService("\082\117\110\083\101\114\118\105\099\101")local q=v.Source.LimbChain local R=require(q)local m=v.Source.LimbChain.FabrikConstraint.BallSocketConstraint local B=require(m)local s=v.Source.LimbChain.FabrikConstraint.HingeConstraint local g=require(s)local i=v.Source.LimbChain.FabrikConstraint.RigidConstraint local W=require(i)local L=workspace.LowerBody local w={"\076\085\112\112\101\114\076\101\103","\076\075\110\101\101","\076\076\111\119\101\114\076\101\103";"\076\070\101\101\116"}local k={"\082\085\112\112\101\114\076\101\103","\082\075\110\101\101";"\082\076\111\119\101\114\076\101\103";"\082\070\101\101\116"}R.convertNamesToMotor6DInstancesInModel(L,w,k)local c=R.new(w,true)local r=RaycastParams.new()local C=workspace:WaitForChild("\082\097\121\070\105\108\116\101\114\070\111\108\100\101\114")r.FilterDescendantsInstances={L,C}c.FootPlacementRaycastParams=r c.LengthToFloor=20 c.FootBottomAttachment=L.LeftLeg.LFeet.FootBottom c.FootBottomRightAttachment=L.LeftLeg.LFeet.FootBottomRight local X=R.new(k,true)local o=L.Constraints.UpperLegConstraint local M=B.new(o,30,30)local F=L.Constraints.KneeConstraint local f=B.new(F,20,89)local t=L.Constraints.LowerLegConstraint local z=B.new(t,20,89)local K=B.new(o,40,40)local D=g.new(F,80,80)local x=g.new(t,100,100)local b={M;D;x}local u={K;f;z}c.PrimaryLimbConstraintTable=b c.SecondaryLimbConstraintTable=u local a=L.ConstraintZones.LeftLegPart1 local y=L.ConstraintZones.LeftLegPart2 local E={a;y}c.PrimaryConstraintRegionFromParts=E local G=L.Constraints.rUpperLegConstraint local l=B.new(G,30,30)local P=L.Constraints.rKneeConstraint local Q=B.new(P,20,90)local N=L.Constraints.rLowerLegConstraint local H=B.new(N,20,80)local e=B.new(G,40,40)local Z=g.new(P,80,80)local T=g.new(N,100,100)local U={e,Z,T}local j={l;Q,H}X.PrimaryLimbConstraintTable=U X.SecondaryLimbConstraintTable=j local J=L.ConstraintZones.RightLegPart1 local I=L.ConstraintZones.RightLegPart2 local n={J;I}X.PrimaryConstraintRegionFromParts=n local Y=Vector3.new(0,-20,0)X:InitDragDebug()d.Heartbeat:Connect(function(v)local d=workspace.MechLTarget.Position local q=workspace.MechRTarget.Position local R=workspace:Raycast(d,Y,r)if R then c:IterateOnce(R.Position,.1)end c:UpdateMotors()end)
--Example script for controlling the left leg of a model in the workspace

--Get service
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

--Modules required
local IKControllerPointer = ReplicatedStorage.Source.LimbChain
local LimbChain = require(IKControllerPointer)

-------------------Import all the Constraints Types-----------------

--BallSocketConstraint
local BallSocketConstraintPointer = ReplicatedStorage.Source.LimbChain.FabrikConstraint.BallSocketConstraint
local BallSocketConstraint = require(BallSocketConstraintPointer)

--HingeConstraint
local HingeConstraintPointer = ReplicatedStorage.Source.LimbChain.FabrikConstraint.HingeConstraint
local HingeConstraint = require(HingeConstraintPointer)

--Rigid Constraint
local RigidConstraintPointer = ReplicatedStorage.Source.LimbChain.FabrikConstraint.RigidConstraint
local RigidConstraint = require(RigidConstraintPointer)

----------------------------------------------------------------

-- Pointers
local lowerBody = workspace.LowerBody
--Name of the Motor6D's in order from hip to foot
local leftLegMotors = {"LUpperLeg","LKnee","LLowerLeg","LFeet"}
local rightLegMotors = {"RUpperLeg","RKnee","RLowerLeg","RFeet"}

--Finds the instances for the Limb Chain to be initialized
--converts the names within the tables to motor6d instances within the model
LimbChain.convertNamesToMotor6DInstancesInModel(lowerBody,leftLegMotors,rightLegMotors)

--Initialize the left leg chain
local leftLegChain = LimbChain.new(leftLegMotors,true)

--Foot placement system
local footParams = RaycastParams.new()
local ignoreParts = workspace:WaitForChild("RayFilterFolder")
footParams.FilterDescendantsInstances = {lowerBody,ignoreParts}

leftLegChain.FootPlacementRaycastParams = footParams
leftLegChain.LengthToFloor = 20

--Get all the attachments in the left foot
leftLegChain.FootBottomAttachment = lowerBody.LeftLeg.LFeet.FootBottom
leftLegChain.FootBottomRightAttachment = lowerBody.LeftLeg.LFeet.FootBottomRight
--Initialize the right leg chain
local rightLegChain = LimbChain.new(rightLegMotors,true)

--Testing the constraint
local testBallSocketConstraint = lowerBody.Constraints.UpperLegConstraint
local upperLegBallSocketConstraint = BallSocketConstraint.new(testBallSocketConstraint,30,30)

local kneePart = lowerBody.Constraints.KneeConstraint
local lKneeBallSocket = BallSocketConstraint.new(kneePart,20,89)

local lLegPart = lowerBody.Constraints.LowerLegConstraint
local lLegBallSocket = BallSocketConstraint.new(lLegPart,20,89)

--[[
    Create the alternative constraints which uses hinge
    More restrictive and glitchy close to original joint but better fitting and looks nicer visually
]]
local upperLegBallSocketConstraintAlternative = BallSocketConstraint.new(testBallSocketConstraint,40,40)
local lKneeHinge = HingeConstraint.new(kneePart,80,80)
local lLegHinge = HingeConstraint.new(lLegPart,100,100)

--Set up two constraint tables to allow
local leftLegConstraintsPrimary = {upperLegBallSocketConstraint,lKneeHinge,lLegHinge}
local leftLegConstraintsSecondary = {upperLegBallSocketConstraintAlternative,lKneeBallSocket,lLegBallSocket}

--Set the constraints of the object
leftLegChain.PrimaryLimbConstraintTable = leftLegConstraintsPrimary
leftLegChain.SecondaryLimbConstraintTable = leftLegConstraintsSecondary

--Set the region for the primary constraints
local leftLegRegionPart1 = lowerBody.ConstraintZones.LeftLegPart1
local leftLegRegionPart2 = lowerBody.ConstraintZones.LeftLegPart2
local leftLegRegion = {leftLegRegionPart1,leftLegRegionPart2}

leftLegChain.PrimaryConstraintRegionFromParts = leftLegRegion

--Repeat for the right leg-----------

--Same constraints but for right leg
local rightBallSocketConstraintPart = lowerBody.Constraints.rUpperLegConstraint
local rupperLegBallSocketConstraint = BallSocketConstraint.new(rightBallSocketConstraintPart,30,30)

local rKneePart = lowerBody.Constraints.rKneeConstraint
local rKneeBallSocket = BallSocketConstraint.new(rKneePart,20,90)

local rLegPart = lowerBody.Constraints.rLowerLegConstraint
local rLegBallSocket = BallSocketConstraint.new(rLegPart,20,80)

--[[
    Create the alternative constraints which uses hinge
    More restrictive and glitchy close to original joint but better fitting and looks nicer visually
]]
local rightUpperLegBallSocketConstraintAlternative = BallSocketConstraint.new(rightBallSocketConstraintPart,40,40)
local rKneeHinge = HingeConstraint.new(rKneePart,80,80)
local rLegHinge = HingeConstraint.new(rLegPart,100,100)

--Construct the constraints table
local rightLegConstraintsPrimary = {rightUpperLegBallSocketConstraintAlternative,rKneeHinge,rLegHinge}
local rightLegConstraintsSecondary = {rupperLegBallSocketConstraint,rKneeBallSocket,rLegBallSocket}

--Set the constraints of the object
rightLegChain.PrimaryLimbConstraintTable = rightLegConstraintsPrimary
rightLegChain.SecondaryLimbConstraintTable = rightLegConstraintsSecondary

--Set the region for the primary constraints
local rightLegRegionPart1 = lowerBody.ConstraintZones.RightLegPart1
local rightLegRegionPart2 = lowerBody.ConstraintZones.RightLegPart2
local rightLegRegion = {rightLegRegionPart1,rightLegRegionPart2}
rightLegChain.PrimaryConstraintRegionFromParts = rightLegRegion

--turn on debug mode if u want
--leftLegChain:DebugModeOn(true,true,false)
--rightLegChain:DebugModeOn(false,true,false)

local down = Vector3.new(0,-20,0)
--[[
    Then use the LimbChain object to control the motor every heartbeat
    ]]
rightLegChain:InitDragDebug()

RunService.Heartbeat:Connect(function(step)
        
    --The Goal position
    local goalPosition = workspace.MechLTarget.Position
    local goalRightPosition = workspace.MechRTarget.Position
    local rayResult = workspace:Raycast(goalPosition,down,footParams)
    if rayResult then
       leftLegChain:IterateOnce(rayResult.Position,0.1)
    end
    leftLegChain:UpdateMotors()

    --InitDragDebug does the samething
    --rightLegChain:IterateOnce(goalRightPosition,0.1)
    --rightLegChain:UpdateMotors()

end)

--Below is testing for Iterate until goal, works I guess

--[[
    
for i=1,10000,1 do
leftLegChain:IterateUntilGoal(workspace.MechLTarget.Position,0.1,15)
leftLegChain:UpdateMotors()
wait(1)
end
]]

