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

How Can I Properly Copy An Accessory from One Character To the Next?

I have tested my method for over 1 hour - I figured out how to create the replacement accessory and add a part, and inside the part named "Handle", added the mesh that the hat contains and the attachment. The only problem is the weld. All of the hats that I copy onto another person seems to get stuck in the same place by the neck area, and putting the accessory weld's C0 onto the new accessory's C0 does NOT work.

My script:(error will be in comment)

local plr = game.Players.LocalPlayer
local player = desiredPlayer

for _,v in pairs(player.Character:GetChildren()) do
	if v:IsA("Accessory") then
		local hat = Instance.new("Accessory", plr.Character)
		hat.AttachmentForward = v.AttachmentForward
		hat.AttachmentPos = v.AttachmentPos
		hat.AttachmentRight = v.AttachmentRight
		hat.AttachmentUp = v.AttachmentUp
		local handle = Instance.new("Part", hat)
		handle.Name = "Handle"
		handle.CFrame = plr.Character.Head.CFrame
		handle.Size = v.Handle.Size
		handle.CanCollide = false
		local mesh = Instance.new("SpecialMesh", handle)
		mesh.MeshId = v.Handle.Mesh.MeshId
		mesh.Offset = v.Handle.Mesh.Offset
		mesh.Scale = v.Handle.Mesh.Scale
		mesh.TextureId = v.Handle.Mesh.TextureId
		for _,vv in ipairs(v.Handle:GetChildren()) do
			if vv:IsA("Attachment") then
				local HairAttachment = Instance.new("Attachment", handle)
				HairAttachment.Name = "Attachment"
				HairAttachment.Orientation = vv.Orientation
				HairAttachment.Position = vv.Position
				HairAttachment.Axis = vv.Axis
				HairAttachment.SecondaryAxis = vv.SecondaryAxis
			end
		end
		local AccessoryWeld = Instance.new("Weld", handle)
		AccessoryWeld.Part0 = plr.Character["Head"]
		AccessoryWeld.Part1 = handle
		AccessoryWeld.C0 = v.Handle.AccessoryWeld.C0 -- everything works fine up until this point. the welds attach to the head, but NOT IN THE RIGHT POSITION!! I can't figure out any alternative to this!!
		AccessoryWeld.C1 = v.Handle.AccessoryWeld.C1
	end
end

2 comments

0

There is this function, but I haven't used it before: http://wiki.roblox.com/index.php?title=API:Class/Humanoid/AddAccessory

0

I am pretty sure, Because I use a system similar to this in my game to parent helmets to the head of players. You need to rotate the handle. Or move the handle. Because in my case everything follows the handle around. So moving or rotating the handle should solve the problem.

1 answer

✓ Accepted answer

I am sorry i did not realize welds went over. ok what i did to tackle this is first i ran a for loop to go through the first character until it found an Accessory. then i looked in the handle to see if i can find the Attachment. One cool thing is Roblox keeps Accessory Attachment's in the Handle and in the Player limbs the same name. so knowing this i wrote a for loop checking each part of player2's character until i found the Attachment named as the first. the code looks scary but in reality its simple.

function cloneHats(player1,player2)
	for _,v in pairs(player1.Character:GetChildren()) do
		if v:IsA("Accessory") then
		local acc= v:Clone()
		print(v.Name)
		local check=acc.Handle:FindFirstChildWhichIsA("Attachment")
		if check then
			for __i,char2 in pairs(player2.Character:GetChildren()) do
				local check2 = char2:FindFirstChild(check.Name)
				if check2 then
					local w = Instance.new("Weld")
					w.Part0 = acc.Handle
					w.Part1 = check2.Parent
					w.C0 = check2.CFrame
					acc.Parent = player2.Character
				end
			end			
		end
		
		end
	end
end

cloneHats(playerFrom,playerTo)
end

3 comments

0

Ok, I admit that would work even more efficient, but will that actually weld/attach the accessory to the character properly?

0

(I tried it, and it requires a weld like I used in my original script, which bring me to my original problem - how am I supposed to find the right position to weld the hat to?

0

THANK YOU! IT WORKED !!

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