How do I instance a ParticleEmitter when Explosion.Visible = false?

Hello this is my first forum post so i’m sorry if i’m doing it wrong.

What I’m trying to do is make a script that Instances a new Explosion every 5 seconds, and based on certain values of an RNG variable, Instance/ do not instance a particle emitter along with the explosion. What I want is for when the explosion is not visible, to instance a particle emitter in the same spot as the explosion. The script shown below is a working script with all of these elements EXCEPT Instancing a particle emitter.
What I need help with is Instancing the particle emitter.
My scripting knowledge is poor, so if you have a solution, please explain it in beginner’s terms. (apologies if my script is unoptimised)
Thanks in advance

wait(4)
while true do
	repeat 
		wait(5)
		local explosion =  Instance.new("Explosion", workspace)
		explosion:SetAttribute("Assigned", math.random(1,5))
		local code = explosion:GetAttribute("Assigned") 
		print(code)
		if code == 1 or 4 or 5 then
			explosion.Visible = true
				end
		if code == 2 or  3 then
			explosion.Visible = false
		end	
		if explosion.Visible == false then
			print("Not visible !")
		end
	
	
until
false
end

You have to check that the code is equal to the number each time:

if code == 1 or code == 4 or code == 5 then
    explosion.Visible = true
else
    explosion.Visible = false
end

A bit off topic but you don’t really need an if statement here, this should work as normal:

explosion.Visible = code == 1 or code == 4 or code == 5

my code is clearer now but my question is not answered. my question is how do i instance a particle emitter when the Explosion.Visible = false.

UPDATE: i am now trying to use coroutines to instance my particle emitter. the coroutine does start, but it never yields/resumes. the particle emitter never displays. I also tried using a part instead and setting part.Position but the part also does not appear. (code still messy)

local PE_THREAD = coroutine.create(function()
	print("coroutine active!")
	local emit = Instance.new("ParticleEmitter")
	emit.SpreadAngle = Vector2(360,360)
	emit.Rate = 20
	emit.Shape = Enum.ParticleEmitterShape.Sphere
	emit.Lifetime.Min = 5
	emit.Lifetime.Max = 6
	wait(1)
	emit:Destroy()
	wait(5)
	coroutine.yield()
end)
wait(2)
while true do
	repeat 
		wait(5)
		local explosion =  Instance.new("Explosion", workspace)
		explosion:SetAttribute("Assigned", math.random(1,5))
		local code = explosion:GetAttribute("Assigned") 
		print(code)
		if code == 1 or code == 4 or code == 5 then
			explosion.Visible = true
			end
		if code == 2 or code == 3 then
			explosion.Visible = false
			print("Not visible !")
			coroutine.resume(PE_THREAD)

		end

	until
	false
end

SOLVED: I learned how to use clones and ServerStorage.

1 Like

Explosion:GetPropertyChangedSignal("Visible")

If you need an event listener for the explosion’s “Visible” property.

1 Like