:SetPrimaryPartCFrame problem

Henlo devs! I am workin’ on FPS game and wanted to make gun position to players arm, this is code :

game.ReplicatedStorage.Equip.OnServerEvent:Connect(function(player, weapon)
	local character = player.Character
	local torso = character:WaitForChild("Torso")
	local rarm = character:WaitForChild("Right Arm")
	local rshoulder = torso:WaitForChild("Right Shoulder")
	local weapon = game.ReplicatedStorage.GunModels:WaitForChild(weapon.Name):Clone()
	for _,i in pairs(weapon:GetChildren()) do
		if i:IsA("BasePart") then
			local weld = Instance.new("WeldConstraint")
			weld.Parent = i
			weld.Part0 = i
			weld.Part1 = weapon.PrimaryPart
			i.Anchored = false
			i.CanCollide = false
		end
	end
	for _,i in pairs(weapon:GetDescendants()) do
		if i:IsA("BasePart") then
			local weld = Instance.new("WeldConstraint")
			weld.Parent = i
			weld.Part0 = i
			weld.Part1 = weapon.PrimaryPart
			i.Anchored = false
			i.CanCollide = false
		end
	end
	weapon.Parent = character
	local weld = Instance.new("WeldConstraint")
	weld.Parent = weapon.PrimaryPart
	weld.Part0 = weapon.PrimaryPart
	weld.Part1 = rarm
	weapon:SetPrimaryPartCFrame(torso.CFrame * CFrame.new(0,0,math.rad(-1)))
end)

But this always happens :

Hope you can help me!

2 Likes

Tried editing code :

game.ReplicatedStorage.Equip.OnServerEvent:Connect(function(player, weapon)
	local character = player.Character
	local torso = character:WaitForChild("Torso")
	local rarm = character:WaitForChild("Right Arm")
	local rshoulder = torso:WaitForChild("Right Shoulder")
	local weaponClone = game.ReplicatedStorage.GunModels:WaitForChild(weapon):Clone()
	for _,i in pairs(weaponClone:GetChildren()) do
		if i:IsA("BasePart") then
			local weld = Instance.new("WeldConstraint")
			weld.Parent = i
			weld.Part0 = i
			weld.Part1 = weaponClone.PrimaryPart
			i.Anchored = false
			i.CanCollide = false
		end
	end
	for _,i in pairs(weaponClone:GetDescendants()) do
		if i:IsA("BasePart") then
			local weld = Instance.new("WeldConstraint")
			weld.Parent = i
			weld.Part0 = i
			weld.Part1 = weaponClone.PrimaryPart
			i.Anchored = false
			i.CanCollide = false
		end
	end
	weaponClone.Parent = character
	local weld = Instance.new("WeldConstraint")
	weld.Parent = weaponClone.PrimaryPart
	weld.Part0 = weaponClone.PrimaryPart
	weld.Part1 = rarm
	weaponClone:SetPrimaryPartCFrame(rshoulder.C1)
end)

And this is result :

1 Like

Alright, so, I feel like I kind of know what’s going on, but I’m not sure, so I’ll just give you my method for doing this.

I would personally run a command to weld every part of the gun to the handle with each gun. I’ll save you some time and do it for you, but make sure to edit it so that it the gun and handle variables actually refer to the gun and handle, and then copy-paste it into the command bar before hitting enter. This will make the script much more efficient, as it won’t need to weld everything while the game is running.

local gun = workspace -- Path to your gun
local handle = gun.Handle -- Change this if the handle is in a different part

for index, value in pairs(gun:GetDescendants()) do
	if value:IsA("BasePart") then
		local weld = Instance.new("WeldConstraint")
		
		weld.Parent = handle
		weld.Part0 = handle
		weld.Part1 = value
		weld.Name = "Handle-" .. value.Name
	end
end

And as for solving this problem, all you would need to do at this point is weld the player’s hand to the gun’s handle, which you probably can do.

(Also, you should delete the :GetChildren() and :GetDescendants() stuff if you try this)

It did not work :confused: bruh. I gotta extend this cuz roblox.

1 Like

Could you send me the command that you ran, as well as the new in-game script?

What do you mean bruh. I already welded gun handle to arm…

1 Like

Finally got it working! I just needed to put :SetPrimaryPartCFrame() before welding!

1 Like

Awesome! Best of luck with the project, man.

Thanks! And I appriceate you tryin’ to help me!

1 Like