aqw559
|
  |
| Joined: 05 Feb 2012 |
| Total Posts: 5 |
|
|
| 20 Nov 2017 12:37 PM |
I recently wanted to learn lua and I'm getting into it, although im currently stuck. I've made a classic cannon that fires upon a mouseclick.
I want the projectile to explode if it only hit a part with a certain name, I know it has to do something with: PartName.Touched:connect()
|
|
|
| Report Abuse |
|
|
|
| 20 Nov 2017 12:52 PM |
You're right about a touched connect, however not exactly how you specified. Thing is with this, I'm going to leave it to you to figure out how to insert an explosion into the part as I want this to be somewhat of a lesson rather than just me handing you a complete script.
function ExplodeOnTouch(hit) if hit.Name == "PartName" then -- This would be the partname print("ka-boom") -- Currently prints "ka-boom" into console whenever script.Parent touches a part with 'PartName'. end end
script.Parent.Touched:connect(ExplodeOnTouch) |
|
|
| Report Abuse |
|
|
Blockzez
|
  |
| Joined: 11 Jun 2017 |
| Total Posts: 15 |
|
|
| 20 Nov 2017 12:54 PM |
Like this? (Insert Part Object here).Touched:connect(function() local Explosion = Instance.new("Explosion") Explosion.Position = (Insert Part Object here).Position Explosion.Parent = (Insert Part Object here) end) |
|
|
| Report Abuse |
|
|
Bonys02
|
  |
| Joined: 05 Dec 2009 |
| Total Posts: 37 |
|
|
| 20 Nov 2017 01:17 PM |
Touched event function need the touched part as parameter Part.Touched:connect(function(hit) end) |
|
|
| Report Abuse |
|
|
aqw559
|
  |
| Joined: 05 Feb 2012 |
| Total Posts: 5 |
|
|
| 20 Nov 2017 01:22 PM |
function ExplodeOnTouch(hit) if hit then local Explosion = Instance.new("Explosion") Explosion.Parent = game.Workspace Explosion.Position = Ball.Position wait(1) Ball:Destroy() end end
Wall.Touched:connect(ExplodeOnTouch)
I assume? Yet nothing happens. |
|
|
| Report Abuse |
|
|
|
| 20 Nov 2017 01:49 PM |
| You're better off making the parent the Ball, not workspace. |
|
|
| Report Abuse |
|
|
aqw559
|
  |
| Joined: 05 Feb 2012 |
| Total Posts: 5 |
|
|
| 20 Nov 2017 01:59 PM |
--//Cannon Ball local Wall = game.Workspace.WallModel.Wall local Ball = game.ServerStorage.CannonBall
function ExplodeOnTouch(hit) if hit then local Explosion = Instance.new("Explosion") Explosion.Parent = Ball.Parent Explosion.Position = Ball.Position wait(1) Ball:Destroy() end end
Wall.Touched:connect(ExplodeOnTouch)
This is the whole script the cannonball is using, I tried changing the parent from workspace to the ball if that's what you meant. Still didn't work. |
|
|
| Report Abuse |
|
|
|
| 20 Nov 2017 02:16 PM |
function ExplodeOnTouch(hit) if hit.Name == "CannonBall" then print("ka-boom") end end
script.Parent.Touched:connect(ExplodeOnTouch)
Put this into your wall, test to see if the console prints "ka-boom" first. |
|
|
| Report Abuse |
|
|
Blockzez
|
  |
| Joined: 11 Jun 2017 |
| Total Posts: 15 |
|
|
| 20 Nov 2017 02:18 PM |
(Update to my post) Fixed: (Insert Part Object here).Touched:connect(function(hit) if hit ~= game.Players.LocalPlayers.Character.Humanoid then return end local Explosion = Instance.new("Explosion") Explosion.Position = (Insert Part Object here).Position Explosion.Parent = (Insert Part Object here) end) |
|
|
| Report Abuse |
|
|
aqw559
|
  |
| Joined: 05 Feb 2012 |
| Total Posts: 5 |
|
|
| 20 Nov 2017 02:37 PM |
I did what you told me and it did not work, I can provide you with the Cannons script too.
--//Cannon local Barrel = game.Workspace.Cannon.Barrel local Button = game.Workspace.Cannon.Button local Ball = game.ServerStorage.CannonBall local BallCopy = Ball:Clone()
Button.ClickDetector.MouseClick:connect(function() if Button then BallCopy.Parent = game.Workspace BallCopy.Position = Barrel.Position BallCopy.Velocity = Vector3.new(110,0,0) end end) |
|
|
| Report Abuse |
|
|
|
| 20 Nov 2017 02:49 PM |
| The console doesn't print kaboom? That's strange because I have a part called Wall which contains the script and I am dropping a part called CannonBall on it now and its printing ka-boom. |
|
|
| Report Abuse |
|
|
aqw559
|
  |
| Joined: 05 Feb 2012 |
| Total Posts: 5 |
|
|
| 20 Nov 2017 03:29 PM |
| I got it working now, thanks for putting down your time to help me. |
|
|
| Report Abuse |
|
|
| |
|