Touched Randomly works

I have this local script that works correctly until I activate the tool while “hitpart” its touching the hitbox ,sometimes it dosent print “hit” it appears to happen randomly how could i fix this?

Thanks for the help!

script.Parent.Activated:Connect(function()
	
	swinging = script.Parent.Swinging

	script.Parent.HitPart.Touched:Connect(function(hit)
		

		if hit.Name == "Hitbox" and swinging.Value == true  then

			print("hit")

		end

	end) 

end)
1 Like

You can’t, not unless you adopt a different method for doing this. It’s just the way Touched works, parts must physically intersect for it to fire. If parts are already intersecting when you hook the connection, it won’t fire.

Another thing: I recommend against connecting a Touched event each time Activated fires because you’ll end up spawning several connections. Either disconnect the Touched function when no longer required or use a variable to determine when hits should be active.

2 Likes

The Touched event only fires when a part is simulating physics. Sleeping or grounded parts are not included.

I wouldn’t recommend this, but if you want to “force” the activity of a part you can apply a small velocity to the part each Heartbeat you want to activate it for. I’d recommend alternating a vertical velocity per frame to make sure the player does not slide around.

Note: Very small velocities are treated as non existent. I’d say a magnitude of .5 is good if you plan on doing this. You can additionally use a BodyVelocity if you don’t want to work with Heartbeat.

Alternatively you can use RayCasting, or GetTouchingParts (which returns all intersecting parts) which I’d recommend doing on Heartbeat.

I recommend checking this post out. It uses raycasting to make the sword and it’s very easy to use.

You just need to initialize it then you can use the HitStart and HitStop methods to determine whether it should deal damage or not. The class has a OnHit event that fires when you hit something. You can use this event to determine who to deal damage to.