Help With GetTouchingPart

I have two scripts to make a sword swing with animations and then and make the tool go to the player’s back when unequipped.

I am trying to make a system so I can just make two scripts for all the tools. I can do this by using CollectionService.

Problem: I don’t know how to go about doing this. I have look and can’t find what I am looking for. I know how to use CollectionService but I don’t know how to do it for this system. The tools are in replicated storage and get cloned to the players backpack when they equip it from their inventory.

How would I go about doing this?

Server script inside tool
local function onActivated(Hit)
	script.Disabled = true
	workspace:WaitForChild("BladeBall").MainScript.Disabled = false
	wait(0.35)
	script.Disabled = false
	workspace:WaitForChild("BladeBall").MainScript.Disabled = true
end

script.Parent.Activated:Connect(onActivated)

local Tool = script.Parent

local Character = Tool.Parent.Parent.Character or Tool.Parent.Parent.CharacterAdded:Wait()
local Torso = Character:FindFirstChild("Torso") or Character:WaitForChild("Torso")
Tool.Equipped:Connect(function()
	for Index, Child in pairs(Torso:GetChildren()) do
		if Child:IsA("Model") and Child.Name == "ToolBackModel" then
			Child:Destroy()
		end

		if Child:IsA("Weld") and Child.Name == "ToolBackWeld" then
			Child:Destroy()
		end
	end
end)

Tool.Unequipped:Connect(function()
	local ToolClone = Tool:Clone()
	local ToolBackModel = Instance.new("Model")
	ToolBackModel.Parent = Torso
	ToolBackModel.Name = "ToolBackModel"

	for Index, Child in pairs(ToolClone:GetChildren()) do
		if Child:IsA("BasePart") or Child:IsA("MeshPart") or Child:IsA("UnionOperation") then
			Child.Parent = ToolBackModel
			Child.CanCollide = false
			Child.Massless = true
		end
	end

	ToolBackModel.PrimaryPart = ToolBackModel:FindFirstChild("Handle")
	local ToolBackWeld = Instance.new("Weld")
	ToolBackWeld.Name = "ToolBackWeld"
	ToolBackWeld.Parent = Torso
	ToolBackWeld.Part0 = Torso
	ToolBackWeld.Part1 = ToolBackModel:FindFirstChild("Handle")
	ToolBackWeld.C1 = CFrame.new(0, 2, -0.5) * CFrame.Angles(0, 0, math.rad(135)) --  Be sure to use math.rad() for CFrame.Angles() since it uses radians not degrees
end)
Local script inside tool's handle
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local tool = script.Parent -- assuming the LocalScript is inside the sword tool

local animations = {
	"rbxassetid://15388446655",
	"rbxassetid://15388455036",
	"rbxassetid://15388460473",
	"rbxassetid://15402133821",
	"rbxassetid://15402137447"
}

local cooldownTime = 0.5 -- Set the cooldown time in seconds
local lastAnimationTime = 0

local function playRandomAnimation()
	local currentTime = tick()

	if currentTime - lastAnimationTime >= cooldownTime then
		local randomIndex = math.random(1, #animations)
		local animationId = animations[randomIndex]

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			local animation = Instance.new("Animation")
			animation.AnimationId = animationId
			local animationTrack = humanoid:LoadAnimation(animation)
			animationTrack:Play()
			local sound = tool.Slash
			sound:Play()
		end

		lastAnimationTime = currentTime
	end
end

local function onBladeBallTouched()
	local particleEmitter = tool.Attachment.Particle
	particleEmitter:Emit(1)
end

mouse.Button1Down:Connect(function()
	tool.Touched:Connect(function(hit)
		if hit.Name == "BladeBall" then
			onBladeBallTouched()
		end
	end)
	playRandomAnimation()
end)

You need to listen for new tagged instances using the GetInstanceAddedSignal method in CollectionService.

I wrote a function a while back to handle this for me:

local defaultAncestors = { game:GetService("Workspace") }

local function bindToTag(
	tag: string,
	callback: (Instance) -> (() -> ())?,
	instAncestors: { Instance }?
): CleanupTagFunction
	local ancestors = (instAncestors or defaultAncestors) :: { Instance }

	local destructors = {}

	local function instanceAdded(instance)
		for _, ancestor in ancestors do
			if instance:IsDescendantOf(ancestor) then
				destructors[instance] = callback(instance)
				break
			end
		end
	end

	for _, instance in CollectionService:GetTagged(tag) do
		task.defer(instanceAdded, instance)
	end

	local instanceAddedConnection = CollectionService:GetInstanceAddedSignal(tag):Connect(instanceAdded)

	local instanceRemovedConnection = CollectionService:GetInstanceRemovedSignal(tag):Connect(function(instance)
		local destructor = destructors[instance]
		if destructor then
			destructor()
		end

		destructors[instance] = nil
	end)

	return function()
		instanceAddedConnection:Disconnect()
		instanceRemovedConnection:Disconnect()
		for _, destructor in destructors do
			if destructor then
				destructor()
			end
		end
		table.clear(destructors)
	end
end

You simply give it the name of the tag, then pass in a function that will be called for every instance with the tag. Here’s how you might use it for your tools:

bindToTag("Tool", function(tool)
	-- example connection
	local activated = tool.Activated:Connect(function()
		-- activated code here	
	end)

	-- other tool related code here

	-- the returned function is called when the tool is destroyed.
	-- this is where you clean up your tool connections
	return function()
		activated:Disconnect()
	end
end)