How to run function independent of script/

I want to be able to run a function completely independent of the script in which it is run.

I created a spaceship which duplicates an explosion particleeffect when it dies, changing its enabled state and then deleting it after it is done. This particle function is called from a ModuleScript.
The script also deletes the model when it is done. However, deleting the model means the script is also deleted, and cannot run the function completely.

the modulescript
function combat.explode(pos)
	local explodepart = game:GetService("ReplicatedStorage").GameParts.ParticlePart:Clone()
	
	explodepart.Position = pos
	explodepart.Parent = workspace.Effects
	
	spawn(function()
		for i, effect in pairs (explodepart.ExplodeAtt:GetChildren()) do
			effect.Enabled = true
			wait(0.2)
			effect.Enabled = false
			wait(1)
		end
	end)

	debris:AddItem(explodepart, 1.2)
end
the spaceship's script
myHuman.HealthChanged:Connect(function(newhealth)
	if newhealth <= 0 then
		print("Died")
		myHuman:ChangeState(Enum.HumanoidStateType.Dead)
		midpoint:SetAttribute("Units", midpoint:GetAttribute("Units") - myModel:GetAttribute("Tier"))
		
		combat.explode(myBody.Position)
		
		game:GetService("Debris"):AddItem(myModel, 1.2)
	else print("Damaged")
		print(newhealth)
	end
end)
clips of weird stuff happening



There are two different particleemitters, an orange one and a yellow one, which represent the explosion and sparks respectively. However, either only one shows, none show, or they both show multiple times.

How do I go about fixing this

remote events could be a solution

I recommend having the spaceship inside a folder, alongside its script, and when you want to delete the spaceship you can only delete the spaceship object and keep the script, something like this:

SpaceshipExample1

This way the script will still exist and the function will be able to continue running.

I hope this helps! (and that this is what you need)

I fixed it using your idea of separating the script from the group. I instead placed a script which runs all the effects in the part that stores the effects, which is placed in the ReplicatedStorage. The part is then cloned into a folder in the workspace called Effects, and the script inside the part handles all the effects stuff.


Thank you for your time.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.