Touched Events not registering

What are you attempting to achieve?

I am learning the Lua language, in order to do so I am following this guide on the Roblox educational resource page. Get Started with Roblox Education | Documentation - Roblox Creator Hub

I’ve found that the code written in the guide is incorrect at times according to studios syntax corrector.

I’m trying to destroy the objects identified when they are touched by the projectile fired.

What is the issue?

However, nothing happens when they come in contact. I’m doing this in a local script as suggested by the guide.

-- Set up touched event for the projectile
		projectile.Touched:Connect(function(other)
			-- The only collisions we care about are those with enemies and with walls
			if other.Name == "EnemyBall" or other.Name == "Wall" then
				-- Hit an enemy or wall, destroy the projectile and tell the server to destroy its copy
				projectile:Destroy()
				destroyProjectile:FireServer(serverProjectile)
				
				-- Hit an enemy, destroy it and tell the server to destroy it as well
				if other.Name == "EnemyBall" then
					destroyEnemy:FireServer(other)
					other:Destroy()
				end
			end
Gif

https://gyazo.com/8b0066ff3a142c644792b21155d781fb

What solutions have you tried so far?

-I’ve double checked all the syntax and formatting of the code
-I was wondering if this should be done on the server end of things due to exploitation and better efficiency, though I don’t know how I would go about doing that.

6 Likes

Try adding print calls to see if it’s even running in the first place

No output of the print calls when the projectile hits the object.

Is the projectile being moved by physics or being moved by code? BaseParts only respond to .Touched events caused by physics.

1 Like

If you are cloning something, make sure this event is happening to the cloned projectile and not just the original projectile

3 Likes
-- Create a new projectile
	local projectile = projectileTemplate:Clone()
	local playerCFrame = player.Character.PrimaryPart.CFrame
	local direction = playerCFrame.LookVector
	projectile.Position = playerCFrame.Position + direction * PROJECTILE_OFFSET
	projectile.Velocity = direction * PROJECTILE_SPEED
	
	-- Zero out gravity on the projectile so it doesn't fall through the ground
	local mass = projectile:GetMass()
	projectile.BodyForce.Force = Vector3.new(0, 1, 0) * mass * game.Workspace.Gravity
	-- Put the projectile in the workspace
	projectile.Parent = game.Workspace
	print("projectile created")

This is the code that creates the projectile

2 Likes

If .Touched() events aren’t cutting it, using a Region3 could possibly work. I’ve made several hitreg scripts before and have noticed that touched can be a little finicky.

(also if you need to rotate the region3s here’s a nifty module to do it for you!)

Hope that helped!

1 Like

Is the asteroid named “EnemyBall” or “Wall”?

Did you not declare the .Touched event after you made the object? You may of just put a .Touched event for the original projectile instead of declaring it for each of the projectiles you clone?

Where did you place these prints? No prints is really weird.

Is there a TouchInterest parented to the projectile?

The fact that there are no prints means that the event isn’t firing.

@IntellectualBeing touch events sometimes just don’t fire. They’re not the most reliable. You might find luck in creating a region3 around the projectile and when something is found inside that region, you create your own event.

:GetTouchingParts() works too.

your code isnt too bad, its juts that you forgot an extra parenthesis to close (function().

so heres an edited version

        projectile.Touched:Connect(function(other)
		-- The only collisions we care about are those with enemies and with walls
		if other.Name == "EnemyBall" or other.Name == "Wall" then
			-- Hit an enemy or wall, destroy the projectile and tell the server to destroy its copy
			projectile:Destroy()
			destroyProjectile:FireServer(serverProjectile)
			
			-- Hit an enemy, destroy it and tell the server to destroy it as well
			if other.Name == "EnemyBall" then
				destroyEnemy:FireServer(other)
				other:Destroy()
			end
		end
   end) -- add the extra end to close our function can complete our code

just added an extra end) at the end to complete your function.

Unfortunately I am having a hard time understanding. I apologize as I’m not very experienced with this language. I’m following a guide to help me grasp a better understanding.

Here is the completed scripts for those curious.
I was considering they could be on different points on the y access but the Wall is included in the touched event and the projectiles are definitely hitting the wall.

BlasterHandler.lua (1.7 KB)

BlasterLauncher.lua (3.3 KB)

BlasterRemotes.lua (989 Bytes)

Sorry if it was hard to understand, I was trying to point out that you were writing your code and forgot one aspect of the syntax.

Basically when you make a function like
Connect(function() you have to add an end) with a closing parenthesis to close ( at (function() to finish your code, or it wont run properly
so

  thing.Touched:connect(function()
  --do stuff
  end) -- properly finish our function

I read the tutorial you linked in the OP and it said to put the local script in PlayerScripts. Is it in there, or in any other place local scripts can run, such as PlayerGui or Backpack?

@ndrwcswll Oh yes, I see.

@Quaration There isn’t a touch interest , how would I add one?
I tried :GetTouchingParts() as well and no luck.

@Computer2467 I’ll have to experiment with that, looks pretty difficult at first hand.

@liuthemoo It’s named EnemyBall. I used the wall to detect if it was an issue in correlation with the parts being on wrong points Y axis.

@uJordy I did the touched event for every clone of the projectile as I don’t think anything would actually touch the original projectile if it’s in in Replicated Storage

@incapaxian It is a child of StarterPlayerScripts

If there is no TouchInterest that means that the touch event is not connected to the part. Destroying the TouchInterest (i.e by doing :ClearAllChildren() I have ran into that myself) will disconnect the touch connection. Also connections don’t carry over to clones. Along with this :GetTouchingParts() only works when there is a TouchInterest on the part.

1 Like

He mentioned that he declared the touched event function for each clone

1 Like