Roblox Unequip Weld

Ive found this script from a roblox tutorial (as im trying to learn welding ect) that works flawlessly, but how do i make it acheive the same thing but when the player unequipped their tool and not when they join.

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

local referenceDummy : Model = SS.ReferenceDummy

local function weld(partA : BasePart, partB : BasePart, offsetCFrame : CFrame)
	partA.CFrame = partB.CFrame * offsetCFrame

	local weldConstraint = Instance.new('WeldConstraint')
	weldConstraint.Part0 = partA
	weldConstraint.Part1 = partB
	weldConstraint.Parent = partA
end

local function onCharacterAdded(character : Model)
	local newWeld = referenceDummy.AK:Clone()
	local weldPart = character:WaitForChild(newWeld.WeldPart.Value.Name)
	weld(newWeld, weldPart, newWeld.WeldPart.Value.CFrame:Inverse() * newWeld.CFrame)
	newWeld.Parent = 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

Using

Tool.Unequipped:Connect(function() 
 -- define character in the tool and of course define the tool. then call the function.
end)

i know how to do that lol, ive tried it many times with that but doesnt work…

Err…
maybe I’m dumb? o well uh try dis?

local SS = game:GetService('ServerStorage')
local Tool = script.Parent
local Character = Tool.Parent -- idk this might change if this isnt a tool script ill just assume it is

local referenceDummy : Model = SS.ReferenceDummy

local function weld(partA : BasePart, partB : BasePart, offsetCFrame : CFrame)
	partA.CFrame = partB.CFrame * offsetCFrame
	local weldConstraint = Instance.new('WeldConstraint')
	weldConstraint.Part0 = partA
	weldConstraint.Part1 = partB
	weldConstraint.Parent = partA
end

Tool.Unequipped:Connect(function()
	local newWeld = referenceDummy.AK:Clone()
	local weldPart = Character:WaitForChild(newWeld.WeldPart.Value.Name)
	weld(newWeld, weldPart, newWeld.WeldPart.Value.CFrame:Inverse() * newWeld.CFrame)
	newWeld.Parent = Character
end)

its all good i figured it all! thanks for the help