How to detect when part is touched?

I’m trying to make a baseball that hurts you when you get hit with it, but it isn’t detecting the touch event. I don’t know if I’m doing something stupid or there’s something wrong with roblox. Any help is appreciated.

script.Parent:WaitForChild("RemoteEvent").OnServerEvent:Connect(function(player, mouse)
	local part = script.Parent:WaitForChild("Handle"):Clone()
	part.Name = "item"
	part.Parent = game.Workspace
	
	part.Position = player.Character:WaitForChild("RightHand").Position
	
	local speed = 500
	part.CFrame = CFrame.new(player.Character:WaitForChild("RightHand").Position, mouse)
	part.Velocity = part.CFrame.LookVector * speed
	
	part.Touched:Connect(function(hit)
		local hitParent = hit.Parent
		local h = hitParent:FindFirstChild("Humanoid")
		if h then
			h.Health = 0
		end
	end)
	
	script.Parent:Destroy()
end)

This is because .Touched is absolutely terrible for detecting objects going at high speeds
Consider looping GetPartBoundsInRadius or use some sort of Raycasting for detection.

Another option is using the open sourced FastCast Module that’s good for this type of stuff

2 Likes

You can use :GetTouchingParts() on the players body parts and find the part(in this case the basketball) and do your function

1 Like

Does the .Touched thing fire at all? You can print something like print("part touched") in the .Touched function to debug

1 Like

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