Help Deleting Non-Child Welds From Part

i have this script. the problem is that the script is unable to delete all welds from all hitboxes connected to it. this means that the players will end up connecting to the ball together. i believe it has to do with the weld being a child of the hitbox however i cannot change the parent because other scripts are dependant on that. how can i fix this?

local ball = script.Parent

ball.Touched:Connect(function(hit)
	local hitbox = hit.Parent:FindFirstChild('HitboxPart')
	if hitbox then
		if hitbox:FindFirstChild("Weld") then
			ball.CanCollide = true
			hitbox:FindFirstChild("Weld"):Destroy()
			hit.Parent.hasBall.Value = false
		elseif not hitbox:FindFirstChild("Weld") then
			if hit.Parent.Humanoid and hit.Parent.HitboxPart then
				ball.CanCollide = true
				hit.Parent.hasBall.Value = true
				local player = hit.Parent
				local hrp = player:FindFirstChild('HumanoidRootPart')

				local weld = Instance.new("Weld", hitbox)
				weld.Part0 = ball
				weld.Part1 = hrp

				local direction = Vector3.new(0.5,-2.4,-1.5)
				local pos = hrp.CFrame:PointToWorldSpace(direction)
				ball.Position = pos
			end
		end
	elseif hit.Parent.Name ~= "Hitbox" then

	end
end)
1 Like

Try this

hitbox:FindFirstChild("Weld").Enabled = false

it has the same effect, unfortunately :frowning:

I looks like this line is creating a weld to the ball and hrp.

local weld = Instance.new(Weld, hitbox)
weld.Part0 = ball
weld.Part1 = hrp

You are looking for and destroying a weld inside the hitbox.

Is that correct?

the weld is inside the hitbox, yeah, but the ball is welded to the humanoidrootpart. i really dont care which part the ball is welded to so if it has anything to do with that than i can change it but im not sure.

I was saying that you are never deleting the hrp weld if you are only deleting the hitbox weld.

does the hitbox have to be one of the weld parts for it to work? if so, i will change that but right now the hitbox is a parent of the weld.

Oh, I see.

You may need to use a for loop then.

FindFirstChild won’t work with multiple items that have the same name.

for _, weld in pairs (hitbox:GetChildren) do
	if weld then
		weld:Destroy()
	end
end

…or something like that.

nope it still didnt work :(( here is a clip of what is happening if that might give some extra context.

I played around with it.

Not sure I understand your ultimate goal but here is something that allows transfer of a ball from player to player with a weld:

local ball = script.Parent

ball.Touched:Connect(function(hit)
	if hit.Parent.Name ~= "Workspace" then
		ball.CanTouch = false -- deny new request
		
		-- check if it is a character
		
		if hit.Parent:FindFirstChild("HumanoidRootPart") then
			if not hit.Parent.HumanoidRootPart:FindFirstChild("BallWeld") then -- not
				
				-- remove curent owner
				
				local currentOwner = ball.Object.Value
				if currentOwner ~= nil then
					if currentOwner:FindFirstChild("HumanoidRootPart") then	
						if currentOwner.HumanoidRootPart:FindFirstChild("BallWeld") then
							currentOwner.HumanoidRootPart.BallWeld:Destroy()
						end
					end
				end
				task.wait()
				currentOwner = nil
				
				-- make this character new owner
				
				ball.Object.Value = hit.Parent
				local weld = Instance.new("Weld", hit.Parent.HumanoidRootPart)
				weld.Name = "BallWeld"
				weld.Part0 = ball
				ball.CFrame = 	hit.Parent.HumanoidRootPart.CFrame
				weld.Part1 = hit.Parent.HumanoidRootPart
				task.wait()
				weld = nil
			end
		end
		
		task.wait(1) -- give time for player to move away
		ball.CanTouch = true -- allow new request
	end
end)

Screen Shot 2023-11-18 at 10.46.36 PM

ball.rbxl (54.2 KB)

Your script didn’t necessarily help me with anything, however you indirectly taught me about object values. instead of having the current player for the ball, i fixed the other scripts to use the current ball for the players. that way, i was able to fix the weld so it’s parent could still be the ball.

1 Like

I love object values.

Who wants to type a bunch of paths!

1 Like