Skip to content

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.

  • The Sparks emitter from Exercise 12 is in the workspace, with 20 in its Emit box.
  • The LocalScript from Exercise 17 is in StarterPlayerScripts.
StepControlActionResult
1The LocalScriptReplace its contents with the script belowThe script now waits for a click.
2Studio ▸ PlayPress, then click on the floor in the gameTwenty sparks burst at the point clicked.
3The gameClick several places quicklyA burst goes off at each. Earlier bursts carry on while later ones start.
4Studio ▸ StopPressSparks 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)

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 passParticles start
A position, as a Vector3At that position. The emitter keeps its own rotation.
A CFrameAt 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 cameraWhere 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.

OptionWhat it changes for this play
CountThe Emit count.
DelayThe Delay, in seconds.
DurationThe Duration, in seconds.
LinkThe object the particles follow. Pass an instance, or the text "camera".
LinkModeThe Link Mode, as text: "Follow", "Pivot", "Weld", "WeldWithoutRotation" or "RigidLocal". With a Link and no LinkMode, "Weld" is used.
EmitParentWhere 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 handleWhat 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.DurationThe length of the play in seconds.

Other plays of the same emitter are not touched by any of these.

SymptomCauseAction
Nothing bursts, and Output says the item has no positionA Folder was passed.Group the emitters in a Model and pass the Model.
The sparks burst at Sparks, not at the clickThe 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 skymouse.Hit is far away when nothing is under the pointer.Click on a surface.
The burst is one particleThe Emit box of Sparks reads 1.Stop the game and enter 20, or pass a Count.
Nothing appears at allThe Emit box of Sparks reads 0.Stop the game and enter 20, or pass a Count.
ChangeResult
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 characterSparks 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 exerciseAbsoluteEmitAt. The origin. The options table. The handle.
Done beforeThe three lines of a script (Exercise 17). WaitForChild (Exercise 17). The Emit count (Exercise 3). Link Mode (Exercises 7 and 9).
Carried forwardNothing.