Nothing much must be done to start the tutorial. For this tutorial, the player will explode when they click a part. Fun, right? You just have to follow three simple steps:
Create a part in your game.
Insert a ClickDetector in that same part.
Insert a Script in the part as well.
That's it. Just three simple steps.
You don't have to explode the player when they click a part. You can do this for any event, but as said above, it will be for when a player clicks a part for this tutorial (You may also have to change the parameters if you switch up the event).
Open up the script. To detect when the player clicked, the MouseClick event must be used. Insert the following line in the script.
This will fire when the part is clicked, but to explode the player, we must know which player clicked it. To do this, write "player" in the parameters of the event like so.
Now we can figure out which player clicked the part. This is important so we can get the player's character. Why we would need the character will be explained later in this tutorial.
Now that we made the MouseClick event, we must make the explosion. We can use Instance.new() to do this. In the script, inside the MouseClick event, insert the following.
local explosion = Instance.new("Explosion")
This will create an Explosion object. But before moving on with the script, it's best to get some insight about Explosions.
Explosions are cool objects to play around with. They destroy themselves after a few seconds, so thankfully, there's no need to use Debris or to call Destroy() on it to clean it up.
Explosions will immediately kill any players caught in it's BlastRadius. To prevent this, either set it's BlastRadius to 0, or give the player a ForceField.
It's BlastRadius property determines how big the radius of the explosion is. This does not visually change the explosion, however.
It's BlastPressure property determines how much force will be applied to BaseParts caught in it's BlastRadius.
It's Position property determines where the explosion will appear. It is not influenced by the explosion's parent, but rather by world-space. This means that parenting it to a part will not automatically make it appear at the part.
It's Visible property determines whether the explosion visually appears.
Hopefully that wasn't too much to handle. Regardless, it's properties and what they do are very useful to know.
Now that you know a bit more about Explosions, why don't we blow up the player with it?
It's time to change the Explosion's properties. Insert the following below the Instance.new("Explosion") line.
explosion.BlastRadius = 5 --You may chnage this to your liking. explosion.BlastPressure = 500000 --This is actually a bit normal for this property. explosion.Position = player.Character.HumanoidRootPart.Position explosion.Parent = player.Character.HumanoidRootPart
As said above, the explosion's position is determined by world-space, so we need to set it to the HumanoidRootPart's position to make it appear there. This is why we needed to know which player clicked it, so we can get it's character, and HumanoidRootPart.
Now, test your game. If you did everything correctly, you should blow up when you click the part you made. Congrats!
If you don't blow up when you click it, you may have not followed the beginning instructions, and/or didn't write the script right. See below for the entire finished script.
Note: You may notice that the player will keep exploding themselves if they keep clicking the part. A debounce would be necessary for this, but it will not be provided in this tutorial.
Make sure to read the beginning of the tutorial to set this up if you haven't already. It is also best to read the entire tutorial to become more familiar with Explosions.
script.Parent.ClickDetector.MouseClick:Connect(function(player) local explosion = Instance.new("Explosion") explosion.BlastRadius = 5 --You may change this to your liking. explosion.BlastPressure = 500000 --This is actually a bit normal for this property. explosion.Position = player.Character.HumanoidRootPart.Position explosion.Parent = player.Character.HumanoidRootPart end)
As said above, you may notice that the player will keep exploding themselves if they keep clicking the part. A debounce would be necessary for this, but it will not be provided in this tutorial.