What Is PartEmitter?
PartEmitter is a simple easy to use and lightweight Class for emitting 3D particles. As this is the first version of the module there are not a lot of features. Documentation can be found down below.
Benefits?
Initially I created this system for use in my own game. However, since I came to realize that this module is actually quite useful for other interesting effects too.
For example you can create colliding particles, 3D particles, particles that emit light and particles that each have differing acceleration and (my favourite) particles that have other effects like particles or trails.
Module
Here is the current Class: Version 1.0.0
local CFramenew = CFrame.new
local RunService = game:GetService("RunService")
local BasepartEmitter = {}
local EmitterList = {}
local function Stepped(runTime,deltaTime)
for _, Emitter in ipairs(EmitterList) do
Emitter.TimePosition += deltaTime
if Emitter.TimePosition > Emitter.Info.Lifetime then
Emitter.TimePosition -= Emitter.Info.Lifetime
local CurrentParticle = Emitter.Clones[Emitter.Current]
if not CurrentParticle then
warn("Particles are being deleted. Make sure particles are not deleted to avoid lag")
local tempClone = Emitter.Info.Basepart:Clone()
tempClone.CFrame = Emitter.Info.Origin.CFrame
tempClone.AssemblyLinearVelocity = Emitter.Velocity
Emitter.Clones[Emitter.Current] = tempClone
tempClone.Parent = Emitter.Info.Parent
elseif CurrentParticle.Parent == nil then
CurrentParticle.Parent = workspace
else
Emitter.ParticleReset:Fire(CurrentParticle)
end
CurrentParticle.CFrame = Emitter.Info.Origin.CFrame
CurrentParticle.AssemblyLinearVelocity = Emitter.Info.Velocity
Emitter.Current += 1
if Emitter.Current > Emitter.Info.ParticleAmount then
Emitter.Current = 1
end
end
end
end
function BasepartEmitter.new(Basepart: BasePart?, Origin: BasePart? | Attachment?, ParticleAmount: number?, Lifetime: number?)
local Emitter = {
Basepart = Basepart or Instance.new("Part");
ParticleAmount = ParticleAmount or 10;
Lifetime = Lifetime or 2; -- Lifetime controls rate
Enabled = true;
Origin = Origin or workspace:FindFirstChildWhichIsA("Terrain");
Parent = workspace;
Velocity = Vector3.new()
}
local EmitterMetatable = {
_newindex = function()
warn("Tried to add property to object.")
end;
_index = function(Table, index)
print("Indexed")
end
}
local Clones = {}
local ParticleResetEvent = Instance.new("BindableEvent")
Emitter.ParticleReset = ParticleResetEvent.Event
for i = 1, Emitter.ParticleAmount do
local BasepartClone = Emitter.Basepart:Clone()
BasepartClone.CFrame = Emitter.Origin.CFrame
BasepartClone.AssemblyLinearVelocity = Vector3.new()
table.insert(Clones,BasepartClone)
end
setmetatable(Emitter,EmitterMetatable)
table.insert(EmitterList,{
Info = Emitter;
Metatable = EmitterMetatable;
TimePosition = 0;
Current = 1;
Clones = Clones;
ParticleReset = ParticleResetEvent;
}
)
return Emitter
end
RunService.Stepped:Connect(Stepped)
return BasepartEmitter
There are notable a couple of things that probably will be removed or added in the future. This the very first version. Please lmk if you have any things that you would like to have added
How to use
The particle emited has several properties and 1 event and 1 constructor function
Basepart Basepart
Template of the basepart used. Can be anywhere. Currently does not update when this property is changed
number ParticleAmount
Number of particles which will be in existence
number Lifetime
Rate of which the particles will update at. 1 means particles will reset every second
Instance Parent
Controls the parent of the particles
Instance Origin
The attachment or part from where the BasepartEmitter will spawn particles from
Vector3 Velocity
The property controls what the initial velocity of the particles will be
NumberRange Angle
Unimplemented
Events
RBXScriptSignal BasepartEmitted.ResetParticle(Basepart particle)
Fired whenever a particle is reset
Functions
BasepartEmitter BasepartEmitter.new( BasePart BasePart, BasePart Origin, number ParticleAmount, number Lifetime)
Creates a new basepart emitter
Demo
https://gyazo.com/823818ad876b5ade7a36b42d6bddccc2
Code (Not factorized)
local EmitterClass = require(script.Class)
local Particle = workspace.Particle -- I already created a particle with effects and stuff
local Run =game:GetService("RunService")
local Emitter = EmitterClass.new(Particle,workspace.Origin)
Particle.Parent = nil
Emitter.Lifetime = .1
Emitter.ParticleAmount = 10
Run.Heartbeat:Connect(function()
Emitter.Velocity = workspace.Origin.CFrame.LookVector * 100
end)
Emitter.ParticleReset:Connect(function(particle)
particle.Trail.Enabled = false
task.wait()
task.wait()
particle.Trail.Enabled = true
end)
Endnotes
I have a few other ideas for a module. But first I have to finish adding stuff to this one first. If you have any complaints or any ideas for improvements let me know. Have a wonderful day