Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Touch Event on projectile only working on occasion?

I have a very simple touch event set up with a projectile fired by the player that kills the player it hits. Quite simply:

p.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= char then
		db = false -- Debounce
		local hum = hit.Parent.Humanoid
		hum:TakeDamage(25)
		folder:Destroy() -- Where parts of projectile are stored*
		wait()
		db = true
	end
end)

-- *Projectile uses linear interpolation to 'fluctuate' the projectile's movement rather than have it move in a straight line (imagine a bolt of lightning), and so uses multiple parts to move.

The issue I'm having is - I'm afraid - rather ambiguous. The touch event only fires occasionally, peculiarly it seems to fire the first few times and only work rarely after that.

I'm pretty sure there is nothing wrong with the touched event itself (I'm including it just in case I've overlooked something), and I'm inclined to believe it is relating to the use of linear interpolation to fluctuate the projectile's movement using multiple parts and an small offset.

Effectively the part's movement works like this:

for i = 0, distance, step do
	local from = lastPos
	
	local offset = Vector3.new(
		math.random(-offset, offset),		
		math.random(-offset, offset),
		math.random(-offset, offset)	
	)/10
	
	local to = from+-(from-to).unit*step+offset
	
	local p = spell:Clone()
	p.Parent = folder
	p.Size = Vector3.new(p.Size.x, p.Size.y, (from-to).magnitude)
	p.CFrame = CFrame.new(from:Lerp(to, 0.5), to)
	
	local p2 = innerSpell:Clone()
	p2.Parent = folder
	p2.Size = Vector3.new(p.Size.x, p.Size.y, (from-to).magnitude)
	p2.CFrame = CFrame.new(from:Lerp(to, 0.5), to)
	
	game.Debris:AddItem(p, 0.1)
	game.Debris:AddItem(p2, 0.1)
	game.Debris:AddItem(folder, 10)
	lastPos = to

	-- Followed by the touch event I provided above

I appreciate the issue is, as I said, rather ambiguous, but it is so peculiar to me I really don't know what else I can say about it. If there's any more information you'd be interested in don't hesitate to ask.

Any suggestions or thoughts are appreciated.

1 answer

CFramed objects won't be detected by the Touched event since the object would be going through the other.

There are other methods to detect hits, my suggestions using raycasting - sending out rays in a loop and using the FindPartOnRay method for detection.

Though I am not sure how expensive it'd be to send out rays from all directions, If the projectile moves fast enough where the likelihood of a player running into the projectile from side is extremely small, one ray shooting forward should suffice.

More on Raycasting

You could also play with BodyMovers, where the Touched event would work. Though with a motion that your projectile seems to be taking, it'd probably be difficult to match what you have with BodyMovers.

This discussion is preserved as it was. New answers, comments, and votes are closed.