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 make my mesh weld script work with R15 since its not working in general?

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent:findFirstChild("Leg1") == nil then
		local g = script.Parent.Parent.Leg1:clone()
		g.Parent = hit.Parent
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].className == "Part" then
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = hit.Parent["Leftfoot"]
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			h[i].Anchored = false
			h[i].CanCollide = false
		end
		
	end

	if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent:findFirstChild("Leg2") == nil then
		local g = script.Parent.Parent.Leg2:clone()
		g.Parent = hit.Parent
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].className == "Part" then
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = hit.Parent["Rightf"]
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			h[i].Anchored = false
			h[i].CanCollide = false
		end
		
	end
end

script.Parent.Touched:connect(onTouched)

When I tested them out it spawned the mesh, but not on the player, only on the ground. Not really sure what to do. The aim was to get the mesh to weld with the feet of a r15 character.

1 answer

✓ Accepted answer

GENERAL PRACTICE

You can place the function connect in the same line as its definition

Use in pairs in conjunction with a table so you don't need the [index] portion.

You don't need to write the local C0 and C1 if you are only defining them for a single use.

ISSUES

Lua is Case Sensitive, so if you used a StarterCharacter to change the "LeftFoot" and "RightFoot" to become "Leftfoot" and "Rightf" respectively, then you don't need to change those names, otherwise, you have to specify "LeftFoot" and "RightFoot" as is.

During your second loop with "h" make sure the part is a base part, since things like accessories don't have a collidable property.

connect is deprecated, use Connect instead

"findFirstChild" needs to be capitalized to FindFirstChild()

"clone()" needs to be capitalized to Clone()

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil and hit.Parent:FindFirstChild("Leg1") == nil then
		local G = script.Parent.Parent:WaitForChild("Leg1"):Clone()
		G.Parent = hit.Parent

		local M = G:WaitForChild("Middle")
		for B, C in pairs(G:GetChildren()) do
			if C:IsA("BasePart") then
				local W = Instance.new("Weld")
				local CJ = CFrame.new(M.Position)
				W.Part0 = M
				W.Part1 = C
				W.C0 = M.CFrame:inverse() * CJ
				W.C1 = C.CFrame:inverse() * CJ
				W.Parent = M
			end	
		local Y = Instance.new("Weld")
		Y.Part0 = hit.Parent:WaitForChild("LeftFoot")
		Y.Part1 = M
		Y.C0 = CFrame.new(0, 0, 0)
		Y.Parent = hit.Parent:WaitForChild("LeftFoot")
        end

		for I, H in pairs(G:GetChildren()) do
			if H:IsA("BasePart") then
				H.Anchored = false
				H.CanCollide = false
			end
		end
	end
	
	if hit.Parent:FindFirstChild("Humanoid") ~= nil and hit.Parent:FindFirstChild("Leg2") == nil then
		local G = script.Parent.Parent:WaitForChild("Leg2"):Clone()
		G.Parent = hit.Parent

		local M = G:WaitForChild("Middle")
		for B, C in pairs(G:GetChildren()) do
			if C:IsA("BasePart") then
				local W = Instance.new("Weld")
				local CJ = CFrame.new(M.Position)
				W.Part0 = M
				W.Part1 = C
				W.C0 = M.CFrame:inverse() * CJ
				W.C1 = C.CFrame:inverse() * CJ
				W.Parent = M
			end	
		local Y = Instance.new("Weld")
		Y.Part0 = hit.Parent:WaitForChild("RightFoot")
		Y.Part1 = M
		Y.C0 = CFrame.new(0, 0, 0)
		Y.Parent = hit.Parent:WaitForChild("RightFoot")
        end

		for I, H in pairs(G:GetChildren()) do
			if H:IsA("BasePart") then
				H.Anchored = false
				H.CanCollide = false
			end
		end
	end
end)

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