Why does a local function not work but when I make it non-function it works fine?

not really a problem but I just want to know

This is the script I was trying to go for

local function Explode()
	
	local function FX()
		local explosionparticlepart = game:GetService("ReplicatedStorage").FXs.ExplosionFrag:Clone()
		explosionparticlepart.Position = MainPart.Position
		explosionparticlepart.Parent = workspace

		game:GetService("Debris"):AddItem(explosionparticlepart, 5)

		explosionparticlepart.Debris.Enabled = true
		explosionparticlepart.Smoke.Enabled = true
		explosionparticlepart.Sparkle.Enabled = true

		task.wait(0.2)

		explosionparticlepart.Debris.Enabled = false
		explosionparticlepart.Smoke.Enabled = false
		explosionparticlepart.Sparkle.Enabled = false
	end
	
	task.spawn(FX)

This is the script that works

local function Explode()
	
	local explosionparticlepart = game:GetService("ReplicatedStorage").FXs.ExplosionFrag:Clone()
	explosionparticlepart.Position = MainPart.Position
	explosionparticlepart.Parent = workspace

	game:GetService("Debris"):AddItem(explosionparticlepart, 5)

	explosionparticlepart.Debris.Enabled = true
	explosionparticlepart.Smoke.Enabled = true
	explosionparticlepart.Sparkle.Enabled = true

	task.wait(0.2)

	explosionparticlepart.Debris.Enabled = false
	explosionparticlepart.Smoke.Enabled = false

I don’t get how particle works sometimes

Nothing in the console? Have you tried printing to see where the code fails to run? Works fine on my end.

I tried something similar to that, the ‘2b’ never prints for me

The one that works vs the first script that I provided

The script that is running the function Explode may be getting destroyed after it finishes running.

local function Explode()
	local function b()
		print("hello")
		task.wait(0.2)
		print("hi") -- never prints
	end
	task.spawn(b)
end

Explode()
task.wait()
script:Destroy()

Removing the task.spawn makes the main coroutine forced to wait for function Explode to finish before it can destroy the script.

If you could show the code that runs after Explode is called, that would be helpful to verify if this is the issue.

2 Likes

OH, I’ll try that out right now