Exercise 17: Fire the explosion with a key
Purpose. To make the explosion go off in the running game when the player presses a key.
Everything so far has been fired by the HUD, and the HUD exists only while you are editing. The plugin does not run in the game. There, a script fires the effect, with one call that does what the Emit button does. The plugin writes the first version of that script for you.
Before starting
Section titled “Before starting”- The
Explosionfolder from Exercise 16 is in the workspace. - Every emitter in it fires when the folder is selected and Emit is pressed. An emitter that does nothing for the Emit button will do nothing for the script.
Procedure
Section titled “Procedure”| Step | Control | Action | Result |
|---|---|---|---|
| 1 | Explosion | Select | The HUD shows the folder’s emitters. |
| 2 | ≡ ▸ Tools ▸ Emit Code | Press | A panel opens holding a short script that names Explosion. |
| 3 | Emit Code ▸ Run | Press | The explosion plays, exactly as with the Emit button. |
| 4 | Emit Code ▸ Select All | Press, then press Ctrl+C | The script is copied. |
| 5 | Explorer ▸ StarterPlayer ▸ StarterPlayerScripts | Insert a LocalScript and paste the script into it | The LocalScript holds the first script shown below. |
| 6 | The LocalScript | Change it to the second script shown below | The script now waits for a key. |
| 7 | Studio ▸ Play | Press, then press the E key in the game | The explosion goes off each time E is pressed. |
| 8 | Studio ▸ Stop | Press | The plugin’s panels return. |
The script as the plugin writes it at step 4:
local Part_Icles = require(game.ReplicatedStorage:WaitForChild("Part_Icles"))Part_Icles:Activate()
local VFX = workspace.ExplosionPart_Icles:AbsoluteEmit(VFX)The script after step 6:
local UserInputService = game:GetService("UserInputService")local Part_Icles = require(game.ReplicatedStorage:WaitForChild("Part_Icles"))Part_Icles:Activate()
local VFX = workspace:WaitForChild("Explosion")
UserInputService.InputBegan:Connect(function(input, gameProcessed) if gameProcessed then return end if input.KeyCode == Enum.KeyCode.E then Part_Icles:AbsoluteEmit(VFX) endend)What the steps did
Section titled “What the steps did”The module. Part_Icles is the ModuleScript the plugin keeps in ReplicatedStorage. It plays effects in the game. The script loads it with require.
Three lines begin every script that fires an effect:
| Line | What it does |
|---|---|
local Part_Icles = require(...) | Loads the module. |
Part_Icles:Activate() | Starts it. It is needed once on each player’s machine. Calling it again does no harm. |
Part_Icles:AbsoluteEmit(item) | Fires item, as the Emit button would. |
AbsoluteEmit takes one emitter, or anything that holds emitters: a Folder, a Model, a Part. It fires everything inside, each emitter with its own Emit, Delay and Duration. The rule of Exercise 3 holds here too. An emitter whose Emit and Duration are both 0 does not fire.
It gives back a number: the time in seconds until the whole effect has finished. It gives back nothing if some part of the effect never ends.
A LocalScript runs on the player’s own machine. Effects must be fired from one. An effect fired from a server Script is seen by no player. To show an effect to every player, the server tells each player’s LocalScript to fire it. See Show one effect to every player.
WaitForChild. The plugin writes the path to the effect directly, as workspace.Explosion. A LocalScript can start before the workspace has finished arriving, and the path then fails. workspace:WaitForChild("Explosion") waits for it.
If the result differs
Section titled “If the result differs”| Symptom | Cause | Action |
|---|---|---|
Nothing happens on E, and the Output window shows an error on the VFX line | The folder had not arrived when the script ran, or its name is different. | Use WaitForChild, with the folder’s exact name. |
Nothing happens, and Output mentions Activate | Part_Icles:Activate() is missing or comes after the call. | Put it straight after the require line. |
| Some layers play and others do not | Those emitters have Emit 0 and Duration 0. | Stop the game. Select each and enter its Emit count. |
| Nothing happens and Output is empty | The script is a Script, not a LocalScript, or it is not in StarterPlayerScripts. | Use a LocalScript in StarterPlayerScripts. |
| The key works in chat as well | gameProcessed is not checked. | Keep the first if of the script. |
Variations
Section titled “Variations”| Change | Result |
|---|---|
local seconds = Part_Icles:AbsoluteEmit(VFX) then if seconds then task.wait(seconds) end | The script waits until the explosion has finished before it carries on. |
Part_Icles:AbsoluteEnable(VFX) | Everything in the folder is switched on and stays on: a stream from each emitter, a loop from each Animate emitter. Emit, Delay and Duration are not used. |
Part_Icles:AbsoluteDisable(VFX) | Everything in the folder stops making particles. Those in flight finish. |
Part_Icles:Disable(VFX) | Everything in the folder stops, and every particle is removed at once. |
Part_Icles:AbsoluteSetTimescale(VFX, 0.25, 2) | The effect runs at quarter speed for two seconds. |
Enum.KeyCode.F in place of Enum.KeyCode.E | The F key fires it. |
| New in this exercise | Emit Code. The three lines: require, Activate, AbsoluteEmit. A LocalScript and a key press. |
| Done before | A Folder fired as one (Exercises 10, 11, 15 and 16). The rule that Emit 0 and Duration 0 fire nothing (Exercises 3 and 9). The Tools menu (Exercise 10). |
| Carried forward | Keep the LocalScript. Exercise 18 changes it. |