Exercise 18: Sparks where the player clicks
Purpose. To burst sparks at the point in the world the player clicks.
In Exercise 17 the effect went off where it was built. Most effects in a game are wanted somewhere decided at that moment: where a shot lands, where a foot comes down, where the player points. One call plays an effect at a place you give it. The emitter is not moved and not duplicated, and any number of plays can run at once.
Before starting
Section titled “Before starting”- The
Sparksemitter from Exercise 12 is in the workspace, with 20 in its Emit box. - The LocalScript from Exercise 17 is in StarterPlayerScripts.
Procedure
Section titled “Procedure”| Step | Control | Action | Result |
|---|---|---|---|
| 1 | The LocalScript | Replace its contents with the script below | The script now waits for a click. |
| 2 | Studio ▸ Play | Press, then click on the floor in the game | Twenty sparks burst at the point clicked. |
| 3 | The game | Click several places quickly | A burst goes off at each. Earlier bursts carry on while later ones start. |
| 4 | Studio ▸ Stop | Press | Sparks is where it was built. It has not moved. |
local Players = game:GetService("Players")local Part_Icles = require(game.ReplicatedStorage:WaitForChild("Part_Icles"))Part_Icles:Activate()
local sparks = workspace:WaitForChild("Sparks")local mouse = Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function() Part_Icles:AbsoluteEmitAt(sparks, mouse.Hit.Position)end)What the steps did
Section titled “What the steps did”AbsoluteEmitAt(item, origin, options) plays item with its particles starting at origin. The first three lines of the script are those of Exercise 17. Only the call has changed.
The origin can be given in three ways.
| You pass | Particles start |
|---|---|
A position, as a Vector3 | At that position. The emitter keeps its own rotation. |
A CFrame | At that position, turned the way the CFrame is turned. A shower that points up at the emitter points along the CFrame here. |
| A Part, Attachment, Model, Bone or the camera | Where that object is at the moment each particle is made. An effect with a Delay or a Duration keeps starting particles at the object as it moves. |
The origin decides only where particles start. Particles already in flight do not follow it. For that, add a link through the options.
The options are a table. Each one changes this play only and leaves the emitter as it was built.
| Option | What it changes for this play |
|---|---|
Count | The Emit count. |
Delay | The Delay, in seconds. |
Duration | The Duration, in seconds. |
Link | The object the particles follow. Pass an instance, or the text "camera". |
LinkMode | The Link Mode, as text: "Follow", "Pivot", "Weld", "WeldWithoutRotation" or "RigidLocal". With a Link and no LinkMode, "Weld" is used. |
EmitParent | Where the particles of this play are placed. |
The handle. The call gives back two things: a handle for this one play, and its length in seconds.
| On the handle | What it does |
|---|---|
handle:Wait() | Waits until this play has finished. |
handle:SoftDisable() | Stops this play making particles. Those in flight finish. |
handle:Disable() | Stops this play and removes its particles at once. |
handle:SetTimescale(0.25, 2) | Runs this play at quarter speed for two seconds. |
handle:IsAlive() | Says whether this play still has particles or is still making them. |
handle.Duration | The length of the play in seconds. |
Other plays of the same emitter are not touched by any of these.
If the result differs
Section titled “If the result differs”| Symptom | Cause | Action |
|---|---|---|
| Nothing bursts, and Output says the item has no position | A Folder was passed. | Group the emitters in a Model and pass the Model. |
The sparks burst at Sparks, not at the click | The emitter is in Animate mode, or is a kind with no place of its own. | Use an emitter made from a Part, in Emit mode. |
| Nothing bursts on a click in the sky | mouse.Hit is far away when nothing is under the pointer. | Click on a surface. |
| The burst is one particle | The Emit box of Sparks reads 1. | Stop the game and enter 20, or pass a Count. |
| Nothing appears at all | The Emit box of Sparks reads 0. | Stop the game and enter 20, or pass a Count. |
Variations
Section titled “Variations”| Change | Result |
|---|---|
Part_Icles:AbsoluteEmitAt(sparks, mouse.Hit.Position, { Count = 60 }) | Sixty sparks for this play. The emitter’s own count stays 20. |
Part_Icles:AbsoluteEmitAt(sparks, mouse.Hit) | The origin is a CFrame, so the shower is also turned. mouse.Hit faces along the player’s line of sight. |
local handle = Part_Icles:AbsoluteEmitAt(sparks, mouse.Hit.Position) then handle:Wait() | The script waits for this burst to finish before carrying on. |
Part_Icles:AbsoluteEmitAt(sparks, hand, { Link = hand, Duration = 5 }), with hand a part of the character | Sparks stream from the hand for five seconds and stay with it as it moves. |
Part_Icles:EnableEmitAt(sparks, sparks, { Count = 50 }) | A burst of fifty at the emitter’s own place, for this play only. |
| New in this exercise | AbsoluteEmitAt. The origin. The options table. The handle. |
| Done before | The three lines of a script (Exercise 17). WaitForChild (Exercise 17). The Emit count (Exercise 3). Link Mode (Exercises 7 and 9). |
| Carried forward | Nothing. |