How do I Clone() Accessory to Character?

I have a script where the try on button lets you try on the hat or whatever. but the clone just keeps getting put on top. i’m not sure why. here’s my script.


local function equip()
    if selected_item then
        local handle = selected_item:WaitForChild("Handle")
        if handle then
            local human = character:WaitForChild("Humanoid")    
            local equipped_item = selected_item:Clone()
            human:AddAccessory(equipped_item)
        end
    end
end

why does clone keep going to workspace instead of character/ human

2 Likes

Try this, not sure if this will work but;

human:AddAccessory(selected_item:Clone())

It’s basically a more optimised way, but make sure the selected item is an accessory.

1 Like

still gets put to workspace though.

I think what you could try is to set the Parent of the item before adding it to the humanoid


local function equip()
    if selected_item then
        local handle = selected_item:WaitForChild("Handle")
        if handle then
            local human = character:WaitForChild("Humanoid")    
            local equipped_item = selected_item:Clone()
			equipped_item.Parent = character
            human:AddAccessory(equipped_item)
        end
    end
end
1 Like

omg. so i got this error lol

AddAcessory failed: Accessory is already being worn by another character.

i imagine has something to do with me putting the accessory in the dummy and taking it back out. should i delete the touccch interest? or…

I think then you may have to set the parent to workspace since it’s seeing that it’s being worn by someone already cause it’s in a Character, my bad

1 Like

no it’s in workspace. but thinks it’s in a character.

deleting touch interest didn’t work

Even if you set the Parent to character it still was set to workspace?

Put the accessory in Replicated Storage and just parent it to the character through the script who is in Workspace. Make sure the Accessory is not on any character.

Basically what @EmbatTheHybrid said

don’t know if i can do that the way my script is set up… if there’s multiple hats. how will i let the script know which hat to pull out of replicated storage

You can specify that with a parameter in the equip() function, is selected_item a variable you set yourself?

1 Like

yea it is. i’m going to try instance.new an accessory

You could try something like to make it compatible with a parameter for the hat location

local function equip(hat)
    if hat then
        local handle = hat:WaitForChild("Handle")
        if handle then
            local human = character:WaitForChild("Humanoid")    
            local equipped_item = hat:Clone()
			equipped_item.Parent = workspace --Or character if workspace doesn't work
            human:AddAccessory(equipped_item)
        end
    end
end

This is a rough way to do but should do the trick.

Also I don’t know why you’re using WaitForChild for the Handle and checking if it exists without a TImeOut, since it can either infinitely yield or set the variable to the Handle, the variable’s a bit useless, although that’s my take and is unrelated to this

now i’m getting cframe problems out of nowhere

local function open_frame(curr)
	frame:TweenPosition(UDim2.new(), "Out", "Quint", .5, true)
	menu_open = true
	
	local current = mouse.Target
	local ugc_item = current.Parent
	if ugc_item then
		local ugc_clone = ugc_item:Clone()
		ugc_clone.Parent = vpf
		selected = ugc_clone
		
		local vp_camera = Instance.new("Camera")
		vpf.CurrentCamera = vp_camera
		vp_camera.Parent = frame
		vp_camera.CFrame = ugc_clone.CFrame * CFrame.Angles(0, math.rad(180), 0) + (ugc_clone.CFrame.LookVector * 5)
		
		funcs.name_text()
		funcs.price_text()
	end
end

	
local function equip(hat)
    if hat then
        local handle = hat:WaitForChild("Handle")
        if handle then
            local human = character:WaitForChild("Humanoid")    
            local equipped_item = hat:Clone()
			equipped_item.Parent = workspace --Or character if workspace doesn't work
            human:AddAccessory(equipped_item)
        end
    end
end

finely back where i’m started but still nothing on the accessory lol.

Hi, sorry for my late response. I’m guessing you are still having issues, what were the CFrame you were obtaining?

1 Like

i figured it out but thank you. i had to create a remote event and make a script on server

2 Likes

Alright, glad you have found out the problem! If you have any more issues don’t be afraid to make another post!

1 Like