Detect where a tool collides with a part during animation

I am working on a breakable system and I wanted particles when the tool collides with the breakable, but I’ve come to a roadblock with finding out where to put the particle emitter

If it is even possible, I would like for the hitPart particle emitter to be spawned in at the location of the tool colliding with the breakable.

So far, I’ve been unable to find a way to detect the collision

I tried connecting the touched event to a function but it would be generally unreliable and would fire even when just touching the breakable normally. Additionally, when trying to use a boolean it would clash with other tools in a weird way, breaking it.

Below is everything in the script I thought was relevant / needed. If anything else is needed or if I need to clarify something, let me know. This is my first time posting so I’m not very experienced lol.

--services and remote events

local tToolConfigs = {
	Axe = {
		BreakSound = "WoodChop",
		BreakTag = "WoodChop",
		ToolName = "Axe",
		DamageProp = "Axe.Damage",
		Animation = "http://www.roblox.com/asset/?id=77747786413711",
		HitPart = "TreeHit"
	},
	Pickaxe = {
		BreakSound = "RockBreak",
		BreakTag = "RockBreak",
		ToolName = "Pickaxe",
		DamageProp = "Pickaxe.Damage",
		Animation = "http://www.roblox.com/asset/?id=128853357",
		HitPart = "RockHit"
	}
}

local function playAnim(hum, Anim, tool, hitCFrame)
	local anim = Instance.new("Animation")
	anim.AnimationId = Anim
	local track = hum:LoadAnimation(anim)
	task.wait(0.5)
	track:Play()
	print("Axe Pos", tool:FindFirstChild("Axe").Position)
	print("Mouse Pos",hitCFrame.Position)
	
end

-- Other functions...

remote.OnServerEvent:Connect(function(plr, sToolType, partHit, hitCFrame, iActive)
	local cfg = tToolConfigs[sToolType]
	if not cfg then return end

	local debounce = getDebounce(plr, "debounce_" .. sToolType)
	if debounce.Value or iActive == 0 then
		return
	end
	debounce.Value = true

	local success, err = pcall(function()
		local char = plr.Character
		if not char then return end

		local tool = char:FindFirstChild(cfg.ToolName)
		local hum = char:FindFirstChild("Humanoid")
		if not tool or not hum then return end
		

		local percentLeft = partHit:FindFirstChild("PercentLeft")
		local sound = partHit:FindFirstChild(cfg.BreakSound)
		if not percentLeft or not sound then return end

		local dmgVal = getDamageValue(tool, cfg.DamageProp)
		if not dmgVal then return end

		if hum:GetState() == Enum.HumanoidStateType.Jumping or hum:GetState() == Enum.HumanoidStateType.Freefall then
			return
		end
		
		local workPlayer = plr.Character
		for _, v in ipairs(workspace:GetChildren()) do
			if v:FindFirstChild("HumanoidRootPart") and v.Name == plr.Name then
				workPlayer = v.HumanoidRootPart
				break
			end
		end
		
		local isClose = (Vector3.new(workPlayer.Position.X, 0, workPlayer.Position.Z) - Vector3.new(partHit.Position.X, 0, partHit.Position.Z)).Magnitude <= 8
		if not isClose then return end
		
		local isHitting = plr.PlayerGui.DestroyObject.Frame.isHitting
		
		
		isHitting.Value = true
		playAnim(hum, cfg.Animation, tool, hitCFrame)
		task.wait(1)

		local hitPart = ReplicatedStorage:WaitForChild("BreakablesSystem"):WaitForChild(cfg.HitPart):Clone()
		hitPart.CFrame = hitCFrame
		hitPart.Parent = partHit
		task.delay(1, function() hitPart:Destroy() end)

		percentLeft.Value -= dmgVal.Value
		local percent = math.clamp(percentLeft.Value / 100, 0, 1)
		updateUI(plr, percent)

		sound:Play()
		if percentLeft.Value <= 0 then
			print("kil")
		end
		isHitting.Value = false
	end)

	debounce.Value = false
end)

If this is even possible, help would be greatly appreciated, as I’ve been stuck for a few days. Thanks!

3 Likes

I’’m on mobile at the moment, so I can’t give an example script, but you should try using a touched event after an activated event, or even using the particles as a hit box that will trigger the touched event. For the particles, just put it in replicated storage and clone it, then parent it to workspace and set the c frame to a part in the tool. Sorry if anything is wrong, I am also a beginner and would appreciate feedback where necessary!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.