Adding parts to the character model

You can write your topic however you want, but you need to answer these questions:

  1. I want to know a more optimized and version of my current script

  1. I haven’t found any solutions because I don’t really know what I should look up for something like this

basically the script adds fingers to the player model that are animatable, but I want to be able to add more without causing too much clutter in my current script

local rs = game:GetService("ReplicatedStorage")

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local left_pointer = rs:WaitForChild("Models").Hands.Left["Left Pointer"]:Clone()
local left_pointer6D = rs:WaitForChild("Models").Hands.Left["Left Pointer6D"]:Clone()
left_pointer.Parent = char:WaitForChild("Left Arm")
left_pointer6D.Parent = char:WaitForChild("Left Arm")
left_pointer6D.Part0 = char:WaitForChild("Left Arm")
left_pointer6D.Part1 = left_pointer

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

To add animatable parts you need to connect the parts using a Motor6D and then edit animations on a rig with the new limbs connected. Part0 should be towards the root part and Part1 should be the limb.
C0 will determine the point where, with respect to Part0, the animated limb will rotate around.

If you are adding fingers I would create the rig in studio and clone the hand into the character. Then assign the Part0 property to the arm parts.

I know this stuff, I just want to know how to do it more efficiently rather than having to clone each finger and assign each part 0. I should have been more specific

for _,arm in {Char["Left Arm"], Char["Right Arm"]} do
	for _,finger in rs.Fingers:GetChildren() do
		local finger = finger:Clone()
		finger.Joint.Part0 = arm
		finger.Name = arm.Name..finger.Name
		finger.Parent = Char
	end
end

Also why are you getting the char only the first time?

Thanks, I’ll try this out, also that last thing you said, I’m not very good at scripting and I’m still learning

1 Like

Okay in that case, put this code into a function and handle the character like so inside of a server script instead of a local script.

local Players = game:GetService("Players")

local function onPlayerAdded(Player : Player)
	
	local function onCharacterAdded(Char : Model)
		-- handle character
	end
	
	Player.CharacterAdded(onCharacterAdded)
end



Players.PlayerAdded:Connect(onPlayerAdded)

Thanks for the help, but for some reason the script puts 2 sets of five fingers on each hand

Try using pairs since you’re putting both arms in a table.
I use “ipairs” for the fingers because it iterates over the table in order.

for _, arm in pairs({Char["Left Arm"], Char["Right Arm"]}) do
 	for _, finger in ipairs(rs.Fingers:GetChildren()) do
		local clonedFinger = finger:Clone() -- Rename variable to avoid overwritng
 		clonedFinger.Joint.Part0 = arm
 		clonedFinger.Name = arm.Name .. finger.Name
 		clonedFinger.Parent = Char
 	end
end

If you’re only have one finger then I would try without using pairs since you’re only cloning one model.

There is no need to use iterator factories now that Luau has generalized iteration

O wow I wasn’t aware of this, thanks!

1 Like

wait, what does generalized iteration mean?

try this code:

local Players = game:GetService("Players")
local RepS = game:GetService("ReplicatedStorage")


local function onPlayerAdded(Player : Player)

	local function onCharacterAdded(Char : Model)
		
		local hands = RepS:WaitForChild("Models"):WaitForChild("Hands")
		
		for _,arm in {Char["Left Arm"], Char["Right Arm"]} do
			
			local hand = hands[arm.Name:sub(1, arm.Name:len()-5)]
			
			for _,finger in RepS.Fingers:GetChildren() do
				if finger:IsA("BasePart") then
					local finger = finger:Clone()
					local m6d = hand:FindFirstChild(finger.Name.."6D"):Clone()
					m6d.Part0 = arm
					m6d.Part1 = finger
					m6d.Parent = arm
					finger.Parent = Char
				end
			end
			
		end
	end

	Player.CharacterAdded(onCharacterAdded)
	if Player.Character then
		task.spawn(onCharacterAdded, onCharacterAdded)
	end
end



Players.PlayerAdded:Connect(onPlayerAdded)
for _,plr in Players:GetPlayers() do
	task.spawn(onPlayerAdded, plr)
end

Generalized iteration is doing

for i,v in sometable do
  -- stuff
end

instead of doing

for i,v in pairs(sometable) do
  -- stuff
end
-- or 
for i,v in next, sometable, nil do

end

The pairs function returns the next function, the table, and nil.
If you do print(pairs({1,2,3})), it prints
function: 0xe87c106bbf74ead5 ▶ {...} nil