Roblox Welding Issues

Ok, so I have these bandages

And i want them to be put on the character when their HP is below 50.

I have this code which is just for testing the weld

Which for some reason doesnt work, the Attachment names match, and everthing, its just it wont do the motor6D.C0 = attachmentA.CFrame

local PS = game:GetService('Players')
local SS = game:GetService('ServerStorage')

local referenceDummy = SS.RLBRef

local function weld(partA, partB, character)
	if character:IsA("Model") and character:FindFirstChild("Humanoid") then
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		if partA and partB and humanoidRootPart then
			local motor6D = Instance.new("Motor6D")
			motor6D.Parent = partA
			motor6D.Part0 = partA
			motor6D.Part1 = partB

			local attachmentA = partA:FindFirstChild("Attachment")
			if attachmentA then
				motor6D.C0 = attachmentA.CFrame
			else
				warn("Attachment not found on partA")
			end

			motor6D.C1 = humanoidRootPart.CFrame

			return motor6D
		else
			warn("Invalid parts or missing HumanoidRootPart")
		end
	else
		warn("Invalid character")
	end
end

local function onCharacterAdded(character : Model)
	local newWeld = referenceDummy.RightLegBandage:Clone()
	newWeld.Parent = character
	local weldPart = character:FindFirstChild("RightUpperLeg") or character:WaitForChild("RightUpperLeg")
	print(weldPart)
	weld(newWeld, weldPart, character)
end

PS.PlayerAdded:Connect(function(player : Player)
	player.CharacterAdded:Connect(onCharacterAdded)
end)


for i, player in pairs (PS:GetPlayers()) do
	player.CharacterAdded:Connect(onCharacterAdded)


	if (player.Character) then
		onCharacterAdded(player.Character)
	end
end
1 Like

AI

The issue with your welding code seems to stem from how C0 and C1 properties of the Motor6D are being set. These properties define the relative positions and orientations of Part0 and Part1 to the motor itself, but they are not being calculated correctly in your code. Specifically, setting C1 to humanoidRootPart.CFrame is incorrect, as C1 should represent the offset from Part1 to the motor, not a direct assignment of the CFrame.

To fix the welding and ensure it works when attaching objects to characters with HP below 50, I’ll outline a corrected approach and include the health check:

Correcting the Weld Function

  1. Calculate Offsets Correctly: You need to calculate C0 and C1 based on the relative positions and orientations of Part0 and Part1. This is usually done by taking the inverse of one part’s CFrame relative to the other.

  2. Checking HP: To apply the bandage when HP is below 50, you’ll need to check the character’s Humanoid’s Health property.

Here’s an updated version of your code incorporating these changes:

local Players = game:GetService('Players')
local ServerStorage = game:GetService('ServerStorage')

local referenceDummy = ServerStorage.RLBRef

local function weld(partA, partB, character)
	if character:IsA("Model") and character:FindFirstChild("Humanoid") then
		local attachmentA = partA:FindFirstChild("Attachment")
		local attachmentB = partB:FindFirstChild("Attachment")

		if attachmentA and attachmentB then
			local motor6D = Instance.new("Motor6D")
			motor6D.Parent = partA
			motor6D.Part0 = partA
			motor6D.Part1 = partB
			-- Calculate the relative CFrame between partA and partB
			motor6D.C0 = attachmentA.CFrame
			motor6D.C1 = partB.CFrame:ToObjectSpace(partA.CFrame) * attachmentB.CFrame
			return motor6D
		else
			warn("Attachments not found on parts")
		end
	else
		warn("Invalid character")
	end
end

local function onCharacterAdded(character)
	character:WaitForChild("Humanoid") -- Ensure the Humanoid has loaded
	if character.Humanoid.Health < 50 then -- Check if HP is below 50
		local newWeld = referenceDummy.RightLegBandage:Clone()
		newWeld.Parent = character
		local weldPart = character:FindFirstChild("RightUpperLeg") or character:WaitForChild("RightUpperLeg", 5) -- Added a timeout to prevent hanging
		if weldPart then
			weld(newWeld, weldPart, character)
		else
			warn("RightUpperLeg not found")
		end
	end
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end)

-- Handle already connected players
for _, player in pairs(Players:GetPlayers()) do
	if player.Character then
		onCharacterAdded(player.Character)
	end

	player.CharacterAdded:Connect(onCharacterAdded)
end

Key Adjustments:

  • Relative Positioning for C0 and C1: This example assumes both PartA and PartB have attachments for correctly positioning them. If this is not your setup, you’ll need to adjust how C0 and C1 are calculated based on your specific scenario.

  • Health Check: This is now incorporated into the onCharacterAdded function, ensuring the bandage is only applied if the character’s health is below 50.

  • Wait for Essential Components: Added a wait condition for Humanoid to ensure it’s loaded before accessing its properties and added a timeout for waiting for RightUpperLeg to prevent the script from hanging indefinitely.

Make sure that your character model and the objects you’re trying to weld have the correct structure and that all necessary components (like attachments) are in place for this code to work effectively.