Adding sword to character

I am making a game and i need to weld a sword to the players hand in r6. I have this script which does this and the sword is welded and parented to the arm but isnt visible for some reason. Is there something wrong in my code?

local Players = game:GetService("Players")

local function weldSwordToRightHand(character)
	local sword = game.ServerStorage:FindFirstChild("sword")
	if sword then
		local rightHand = character:FindFirstChild("Right Arm")
		if rightHand then
			local swordClone = sword:Clone()
			swordClone.Parent = workspace
			swordClone.CFrame = rightHand.CFrame
			swordClone.Parent = rightHand

			local weld = Instance.new("Weld")
			weld.Part0 = rightHand
			weld.Part1 = swordClone
			weld.C0 = CFrame.new(0, -1, 0) 
			weld.Parent = rightHand
		end
	end
end

local function onCharacterAdded(character)
	if character:FindFirstChild("Humanoid") and character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
		weldSwordToRightHand(character)
	end
end

local function onPlayerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait()
	onCharacterAdded(character)
	player.CharacterAdded:Connect(onCharacterAdded)
end

for _, player in Players:GetPlayers() do
	onPlayerAdded(player)
end

i changed the function that checked for all players on server to a function that gets activated when a player joins and it works


local Players = game:GetService("Players")

local function weldSwordToRightHand(character)
	local sword = game.ServerStorage:FindFirstChild("sword")
	if sword then
		local rightHand = character:FindFirstChild("Right Arm")
		if rightHand then
			local swordClone = sword:Clone()
			swordClone.Parent = workspace
			swordClone.CFrame = rightHand.CFrame
			swordClone.Parent = rightHand

			local weld = Instance.new("Weld")
			weld.Part0 = rightHand
			weld.Part1 = swordClone
			weld.C0 = CFrame.new(0, -1, 0) 
			weld.Parent = rightHand
		end
	end
end

local function onCharacterAdded(character)
	if character:FindFirstChild("Humanoid") and character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
		weldSwordToRightHand(character)
	end
end

local function onPlayerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait()
	onCharacterAdded(character)
	player.CharacterAdded:Connect(onCharacterAdded)
end


game.Players.PlayerAdded:Connect(onPlayerAdded)

1 Like

Thanks, it seems that works now. I was using the ai assistant because I got stuck.

actually that didnt work at all. The problem was that the sword was welded and parented but just not visible and i have no idea why

is the sword in the right place but just invisible?

It was but i fixed it, The only problem is that it is locked in the wrong orientation so i need help with that

you need to set the weld orientation for the part to be rotated

for example:
weld.C0 = CFrame.new(0, -1, 0) *CFrame.Angles(math.rad(90),math.rad(-90),math.rad(90),) --math.rad is for converting radiants to degrees
you might need to change the values for the sword to be rotated in the way you want

1 Like

Yes thank you! This worked perfectly