Scripting help with parts and emitters

Im really new to scripting so bare with me here… The way this works is a player picks up a tool on the map, and needs to deliver the tool to a part. Once the player touches the part, it takes the tool (“CD”) out of the players inventory. The part then plays a sound, and releases a particle emitter for 40 seconds; (duration of the sound) and is then disabled.
The problem I have here, Is that the particle emitter is doing it’s job at disabling itself after 40 seconds using the “wait” function, but the player can start up the particle emitter again, if the player touches the part. This is exactly what I don’t want it to do. I just need the particle emitter to do it’s job for 40 seconds, and never appear again. Any help would be greatly appreciated!

``
– Script for taking a tool out of a player’s inventory

local part = script.Parent
part.Touched:Connect(onTouched)

local function onTouched(other)
local character = other.Parent
local humanoid = character:FindFirstChildWhichIsA(“Humanoid”)

-- If touched by a player's humanoid and the player has the tool, remove it from their inventory
if humanoid then
	local tool = character:FindFirstChild("CD")  -- Replace 'ToolName' with the name of your tool
	if tool then
		tool.Parent = nil

		local soundId = "rbxassetid://15554950523"
		local sound = Instance.new("Sound")
		sound.SoundId = soundId
		sound.Parent = part
		sound:play()

		-- Particle Emitter
		local emitter = Instance.new("ParticleEmitter")
		emitter.Parent = part

		-- Customize the particle effect properties
		emitter.Texture = "rbxassetid://3995331885" -- Replace with the asset ID of your particle texture
		emitter.Rate = 10--Particles per second
		emitter.Lifetime = NumberRange.new(0.5, 0.5)
		emitter.Enabled = true
		local colorKeypoints = {
			-- API: ColorSequenceKeypoint.new(time, color)
			ColorSequenceKeypoint.new(0, Color3.new(0.572549, 1, 0.984314)), -- At t=0, White
			ColorSequenceKeypoint.new(0.5, Color3.new(0.741176, 1, 0.682353)), -- At t=.5, Orange
			ColorSequenceKeypoint.new(1, Color3.new(0.294118, 1, 0.137255)), -- At t=1, Red
		}
		emitter.Color = ColorSequence.new(colorKeypoints)
		local numberKeypoints = {
			-- API: NumberSequenceKeypoint.new(time, size, envelop)
			NumberSequenceKeypoint.new(0, 1), -- At t=0, fully transparent
			NumberSequenceKeypoint.new(0.1, 0), -- At t=.1, fully opaque
			NumberSequenceKeypoint.new(0.5, 0.25), -- At t=.5, mostly opaque
			NumberSequenceKeypoint.new(1, 1), -- At t=1, fully transparent
		}
		emitter.Transparency = NumberSequence.new(numberKeypoints)
		emitter.LightEmission = 1 -- When particles overlap, multiply their color to be brighter
		emitter.LightInfluence = 0 -- Don't be affected by world lighting

		-- Speed properties
		emitter.EmissionDirection = Enum.NormalId.Front -- Emit forwards
		emitter.Speed = NumberRange.new(0, 0) -- Speed of zero
		emitter.Drag = 0 -- Apply no drag to particle motion
		emitter.VelocitySpread = NumberRange.new(0, 0)
		emitter.VelocityInheritance = 0 -- Don't inherit parent velocity
		emitter.Acceleration = Vector3.new(0, 0, 0)
		emitter.LockedToPart = false -- Don't lock the particles to the parent
		emitter.SpreadAngle = Vector2.new(0, 0) -- No spread angle on either axis

		-- Simulation properties
		local numberKeypoints2 = {
			NumberSequenceKeypoint.new(0, 0), -- At t=0, size of 0
			NumberSequenceKeypoint.new(1, 10), -- At t=1, size of 10
		}
		emitter.Size = NumberSequence.new(numberKeypoints2)
		emitter.ZOffset = -1 -- Render slightly behind the actual position
		emitter.Rotation = NumberRange.new(0, 360) -- Start at random rotation
		emitter.RotSpeed = NumberRange.new(0) -- Do not rotate during simulation
	    

		-- Function to activate the particle effect
		local function activateParticleEffect()
			emitter.Enabled = true
			wait(40)  -- Adjust the duration as needed
			emitter.Enabled = false
		end

		-- Connect the Touched event of the part to the activateParticleEffect function
		part.Touched:Connect(activateParticleEffect)

	end
end

end

part.Touched:Connect(onTouched)

``

If I understand correctly, create a variable at the top of the script and set it to false. I am on mobile right now so the formatting is a bit scuffed. You can also try removing the local before the variable if it doesn’t work.

local cooldown = false

local function activateParticleEffect()
if cooldown == false then
cooldown = true
emitter.Enabled = true
task.wait(40) – Adjust the duration as needed
emitter.Enabled = false
end
end

1 Like

You could try disconnecting the part.Touched connection in the activateParticleEffect() function, for example:

        local particleConnection = nil
		local function activateParticleEffect()
            if particleConnection ~= nil then
                particleConnection:Disconnect()
                particleConnection = nil
            end
			emitter.Enabled = true
			wait(40)  -- Adjust the duration as needed
			emitter.Enabled = false
		end

		-- Connect the Touched event of the part to the activateParticleEffect function
		particleConnection = part.Touched:Connect(activateParticleEffect)

Why not destroy the emitter if you won’t need it anymore like so:

-- If touched by a player's humanoid and the player has the tool, remove it from their inventory
if humanoid then
	local tool = character:FindFirstChild("CD")  -- Replace 'ToolName' with the name of your tool
	if tool then
		tool.Parent = nil

		local soundId = "rbxassetid://15554950523"
		local sound = Instance.new("Sound")
		sound.SoundId = soundId
		sound.Parent = part
		sound:play()

		-- Particle Emitter
		local emitter = Instance.new("ParticleEmitter")
		emitter.Parent = part

		-- Customize the particle effect properties
		emitter.Texture = "rbxassetid://3995331885" -- Replace with the asset ID of your particle texture
		emitter.Rate = 10--Particles per second
		emitter.Lifetime = NumberRange.new(0.5, 0.5)
		emitter.Enabled = true
		local colorKeypoints = {
			-- API: ColorSequenceKeypoint.new(time, color)
			ColorSequenceKeypoint.new(0, Color3.new(0.572549, 1, 0.984314)), -- At t=0, White
			ColorSequenceKeypoint.new(0.5, Color3.new(0.741176, 1, 0.682353)), -- At t=.5, Orange
			ColorSequenceKeypoint.new(1, Color3.new(0.294118, 1, 0.137255)), -- At t=1, Red
		}
		emitter.Color = ColorSequence.new(colorKeypoints)
		local numberKeypoints = {
			-- API: NumberSequenceKeypoint.new(time, size, envelop)
			NumberSequenceKeypoint.new(0, 1), -- At t=0, fully transparent
			NumberSequenceKeypoint.new(0.1, 0), -- At t=.1, fully opaque
			NumberSequenceKeypoint.new(0.5, 0.25), -- At t=.5, mostly opaque
			NumberSequenceKeypoint.new(1, 1), -- At t=1, fully transparent
		}
		emitter.Transparency = NumberSequence.new(numberKeypoints)
		emitter.LightEmission = 1 -- When particles overlap, multiply their color to be brighter
		emitter.LightInfluence = 0 -- Don't be affected by world lighting

		-- Speed properties
		emitter.EmissionDirection = Enum.NormalId.Front -- Emit forwards
		emitter.Speed = NumberRange.new(0, 0) -- Speed of zero
		emitter.Drag = 0 -- Apply no drag to particle motion
		emitter.VelocitySpread = NumberRange.new(0, 0)
		emitter.VelocityInheritance = 0 -- Don't inherit parent velocity
		emitter.Acceleration = Vector3.new(0, 0, 0)
		emitter.LockedToPart = false -- Don't lock the particles to the parent
		emitter.SpreadAngle = Vector2.new(0, 0) -- No spread angle on either axis

		-- Simulation properties
		local numberKeypoints2 = {
			NumberSequenceKeypoint.new(0, 0), -- At t=0, size of 0
			NumberSequenceKeypoint.new(1, 10), -- At t=1, size of 10
		}
		emitter.Size = NumberSequence.new(numberKeypoints2)
		emitter.ZOffset = -1 -- Render slightly behind the actual position
		emitter.Rotation = NumberRange.new(0, 360) -- Start at random rotation
		emitter.RotSpeed = NumberRange.new(0) -- Do not rotate during simulation
	    

		-- Function to activate the particle effect
		local function activateParticleEffect()
			emitter.Enabled = true
			game.Debris:AddItem(emitter, 40)
		end

		-- Connect the Touched event of the part to the activateParticleEffect function
		part.Touched:Connect(activateParticleEffect)

	end
end

Thank you Mr. JohhnyLegoKing, the script worked perfectly and haven’t had issues since. Greatly appreciate your help.

1 Like

Glad I could help! Please remember to mark as a solution :wink:

1 Like

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