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

Script won't clone model and How do I create welds for these parts?

local pants = script.Parent.Pants.PantsTemplate
local shirt = script.Parent.Shirt.ShirtTemplate



--------
local function removeAllParts(hit)
	hit.Parent.Head.face.Texture = ""
	hit.Parent.Pants.PantsTemplate = pants
	hit.Parent.Shirt.ShirtTemplate = shirt
	for i, v in pairs(hit.Parent:GetChildren()) do
		if v:IsA("Accessory") then
			v:Destroy()
		end
	end
end

local function createWelds(hit)
	local chr = hit.Parent
	for i,v in pairs(script.Parent.Parent:GetChildren()) do
		if v:IsA("Model") then
			v.PrimaryPart = v.Middle
			local clone = v:Clone()
			if v.Name == "Eyes" then
				clone:SetPrimaryPartCFrame(chr.Head.CFrame)
			elseif v.Name == "Leg1" then
				clone:SetPrimaryPartCFrame(chr["Left Leg"].CFrame)
			elseif v.Name == "Leg2" then
				clone:SetPrimaryPartCFrame(chr["Right Leg"].CFrame)
			elseif v.Name == "Chest" then
				clone:SetPrimaryPartCFrame(chr.Torso.CFrame)
			elseif v.Name == "Arm1" then		
				clone:SetPrimaryPartCFrame(chr["Left Arm"].CFrame)
			elseif v.Name == "Arm2"	then
				clone:SetPrimaryPartCFrame(chr["Right Arm"].CFrame)
				
			end
		end
	end
end

script.Parent.Touched:Connect(function(hit)
	removeAllParts(hit)
	createWelds(hit)
end)

The error arises in the createWelds function, where it doesn't seem to clone the parts, or place them at the characters bodypart positions

1 answer

Try doing this instead: (I don't know if it works or not)

local pants = script.Parent.Pants.PantsTemplate
local shirt = script.Parent.Shirt.ShirtTemplate



script.Parent.Touched:Connect(function(hit)
    local function removeAllParts(hit)
    hit.Parent.Head.face.Texture = ""
    hit.Parent.Pants.PantsTemplate = pants
    hit.Parent.Shirt.ShirtTemplate = shirt
    for i, v in pairs(hit.Parent:GetChildren()) do
        if v:IsA("Accessory") then
            v:Destroy()
        end
    end

    local function createWelds(hit)
        local chr = hit.Parent
        for i,v in pairs(script.Parent.Parent:GetChildren()) do
            if v:IsA("Model") then
                v.PrimaryPart = v.Middle
                local clone = v:Clone()
                
                if v.Name == "Eyes" then
                    clone:SetPrimaryPartCFrame(chr.Head.CFrame)
                elseif v.Name == "Leg1" then
                    clone:SetPrimaryPartCFrame(chr["Left Leg"].CFrame)
                elseif v.Name == "Leg2" then
                    clone:SetPrimaryPartCFrame(chr["Right Leg"].CFrame)
                elseif v.Name == "Chest" then
                    clone:SetPrimaryPartCFrame(chr.Torso.CFrame)
                elseif v.Name == "Arm1" then        
                    clone:SetPrimaryPartCFrame(chr["Left Arm"].CFrame)
                elseif v.Name == "Arm2" then
                    clone:SetPrimaryPartCFrame(chr["Right Arm"].CFrame)
                end
            end
        end
    end

    removeAllParts(hit)
    createWelds(hit)
end)

Also you might need to parent some stuff since the script doesn't know where to put it.

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