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
-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.
-- 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")
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!)
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?
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.
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.
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?
@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
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.