Skip to content

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.

  • The Explosion folder 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.
StepControlActionResult
1ExplosionSelectThe HUD shows the folder’s emitters.
2≡ ▸ Tools ▸ Emit CodePressA panel opens holding a short script that names Explosion.
3Emit Code ▸ RunPressThe explosion plays, exactly as with the Emit button.
4Emit Code ▸ Select AllPress, then press Ctrl+CThe script is copied.
5Explorer ▸ StarterPlayer ▸ StarterPlayerScriptsInsert a LocalScript and paste the script into itThe LocalScript holds the first script shown below.
6The LocalScriptChange it to the second script shown belowThe script now waits for a key.
7Studio ▸ PlayPress, then press the E key in the gameThe explosion goes off each time E is pressed.
8Studio ▸ StopPressThe 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.Explosion
Part_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)
end
end)

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:

LineWhat 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.

SymptomCauseAction
Nothing happens on E, and the Output window shows an error on the VFX lineThe 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 ActivatePart_Icles:Activate() is missing or comes after the call.Put it straight after the require line.
Some layers play and others do notThose emitters have Emit 0 and Duration 0.Stop the game. Select each and enter its Emit count.
Nothing happens and Output is emptyThe script is a Script, not a LocalScript, or it is not in StarterPlayerScripts.Use a LocalScript in StarterPlayerScripts.
The key works in chat as wellgameProcessed is not checked.Keep the first if of the script.
ChangeResult
local seconds = Part_Icles:AbsoluteEmit(VFX) then if seconds then task.wait(seconds) endThe 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.EThe F key fires it.
New in this exerciseEmit Code. The three lines: require, Activate, AbsoluteEmit. A LocalScript and a key press.
Done beforeA 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 forwardKeep the LocalScript. Exercise 18 changes it.