Parts not being set the correct position with CFrame

Hello! As the title suggests, I’m facing an issue with my script where the cloned parts aren’t getting assigned the correct CFrame. I’ve customized a vest giver script to fit my needs. However, during testing, I’ve noticed that only one of the parts is being moved correctly, while the rest remain in their original positions. It’s puzzling because I’ve used the exact same script in other games multiple times, and it worked perfectly. I’m pretty sure the problem lies in how I’m calling the function, but I’m unsure about an alternative approach. I really appreciate your assistance.

asdasdasdasdasdasdasdasdasdasdads

Here is my script:

local plrs = game:GetService("Players")
local tweenService = game:GetService("TweenService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")
local toc_rs = replicatedStorage:WaitForChild("TOC_ReplicatedStorage")
local toc_ss = serverScriptService:WaitForChild("TOC_ServerScriptService")

local squadsFolder = toc_rs:WaitForChild("Squads")
local teamsFolder = toc_rs:WaitForChild("Teams")
local spawnsFolder = toc_rs:WaitForChild("Spawns")
local matchStats = toc_rs:WaitForChild("MatchStats")
local events = toc_rs:WaitForChild("Events")

local function weld(plr, model, limb)
	if not plr.Character:FindFirstChild(model) then
		local g = model:Clone()
		g.Parent = plr.Character
		g.Name = "Gear"

		local C = g:GetChildren()
		for i=1, #C do
			if C[i]:IsA("Part") or C[i]:IsA("WedgePart") or C[i]:IsA("MeshPart") or C[i]:IsA("UnionOperation") 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 = plr.Character[limb]
			Y.Part1 = g.Middle
			Y.C0 = CFrame.new(0, 0, 0)
			Y.Parent = Y.Part0
		end		
	end
end

local function setKit(plr, role, team)
	local kitsFolder = teamsFolder:FindFirstChild(team).Infantry.Roles
	local currentRole = kitsFolder:FindFirstChild("Medic")
		
	for _, v in pairs(kitsFolder:GetChildren()) do
		if v.Name:find(role) then
			currentRole = v
						
			for _, v2 in pairs(currentRole.Gear:GetChildren()) do
				weld(plr, v2, v2.Name)
			end
		end
	end
end

events.DeployEvent.OnServerEvent:Connect(function(plr, role, team, spawnLocation)
	if spawnLocation then
		if role then				
			setKit(plr, role, team)
		end
	end
end)

Solved the issue. It was the loop that was the issue.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.