Touched event working sometimes

Hi people,
i am making a punching mechanic and am not looking to make it so advanced or anything.
Everything is setup correctly and works fine. The only thing that wont work correctly and how i want, is that the touched event sometimes doesnt detect a hit or a colission. I was told many billion times to not use touched events but thats simply the only way ik how to make it work, my script is:

box.Touched:Connect(function(hit)
			local p = hit.Parent:FindFirstChildWhichIsA("Humanoid")
			if hit.Parent.Name == charName then
				return
			else
				if not deb  then
					deb = true
					if p then
						p:TakeDamage(25)
					end
					task.wait(1)
					deb = false
				end
			end
		end)
		task.wait(1)
		box:Destroy()

Please help me fix this problem.

4 Likes

:GetTouchingParts()

Use that, it returns a table of everything that is touching it currently, to which you can use a for statement to see if it touched a character.

1 Like

Or use raycasting hitbox, it works too.

1 Like

Never heard of it, how would that look like?

Something like this:

for i, part in touching do
			if part.Parent:FindFirstChildWhichIsA("Humanoid") then
			local	p = part.Parent:FindFirstChildWhichIsA("Humanoid") then
			end
		end

??

okay so when you spawn the hitbox, it should look something like this:

local parts = hitbox:GetTouchingParts()

for i, v in pairs(parts) do
   if v.Parent:FindFirstChildWhichIsA("Humanoid") and v.Parent.Name ~=  then -- howeverugetthenameofthecharacter 
-- code here
   end
end

would be like this

Get touching parts returns a table btw, it’s not like a single instance.

then how the hell would i do damage to player. I tried this:

for i, v in pairs(parts) do
			if v.Parent:FindFirstChildWhichIsA("Humanoid") and v.Parent.Name ~= charName then -- howeverugetthenameofthecharacter 
				local p = v.Parent:FindFirstChildWhichIsA("Humanoid")
				p:TakeDamage(25)
			end
		end

note: does not work

Try using a print statement, see what it is getting in the table.

Like at the top do like print(v.Name).

Does not print anything at all

Oh wait nvm :GetTouchingParts() uses cancollide I think, woops.

Try raycasting hitbox.

Correct. Like you were told, the “Touched” event is highly unreliable and works very poorly. You should therefore as other people are gonna tell you within this post, use another method e.g Raycasting if it’s usage would be fine in your code.

Okay thanks i will try ,.,.,.,.,.,

How, how is that possible? Isnt raycast like shooting ray or something

Have a few rays come out of the character, if it hits a instance, it will detect it and return that instance with all of the values. Here is the documentation for it: Raycasting | Documentation - Roblox Creator Hub

But how can i do it so when the part gets touched

How does the hitbox work? Does it always exist and only activate on click? Or does it like do Instance.new()?

well, thats in a function that activates as soon the tool gets activated.

Okay so when the tool gets activated, fire the raycasts.