Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i make this particle emitter toggle on and off?

This is what i attempted to do but it enables and disables the particle emitter instantly. Trying to make it so that the particles would be enabled when i press Z and it'll disable once i press Z again.

--//Services
local UIS = game:GetService('UserInputService')
local serverStorage = game:GetService('ServerStorage')
local replicatedStorage = game:GetService('ReplicatedStorage')

--//Variables
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild('Humanoid')
local root = char:WaitForChild('HumanoidRootPart')

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		local clone = replicatedStorage:WaitForChild('Particle'):Clone()
		clone.Parent = game.Workspace
		clone.Position = root.Position
		clone.ParticleEmitter.Enabled = true
		
		local weld = Instance.new('Weld',clone)
		weld.Part0 = clone
		weld.Part1 = root
		
	if clone.ParticleEmitter.Enabled == true then
		if input.KeyCode == Enum.KeyCode.Z then
			clone.ParticleEmitter.Enabled = false
		end
		end
	end
end)

2 comments

0

try adding a wait

0

It's cause you set the particle emitter to true in line 17 and to false in line 25. Everytime you press Z it will always make it enabled then disabled.

2 answers

✓ Accepted answer

Your original attempt does not work because you set the particle emitter enabled to true in line 17 and since the next if statement checks if the emitter is enabled, and you just set the enabled to true, it passes the condition and makes it false directly. I modified your script so that it will check if the part that has the emitter on if it exists in the workspace, and if it doesn't, I create that part by cloning. Else, if it already exists,I just toggle the emitter inside it on and off.

I left the part parented to the workspace, but I recommend parenting it to the character instead, so if you want to do this, change line 16 and 29 on the fixed script from workspace to char.

Your script will also only work locally, and any other people will not be able to see the particle emitter unless you use RemoteEvents.

--//Services
local UIS = game:GetService('UserInputService')
local serverStorage = game:GetService('ServerStorage')
local replicatedStorage = game:GetService('ReplicatedStorage')

--//Variables
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild('Humanoid')
local root = char:WaitForChild('HumanoidRootPart')
local on = false

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		--Find if the particle exists or not.
		local particlepart = workspace:FindFirstChild("Particle")
		--If the emitter exists and is on turn it off, and likewise.
		if particlepart and on then
			particlepart.ParticleEmitter.Enabled = false
			on = false
		elseif particlepart and not on then
			particlepart.ParticleEmitter.Enabled = true
			on = true
		else
		--If the emitter does not exist, create a new emitter.
			local clone = replicatedStorage:WaitForChild('Particle'):Clone()
			clone.Position = root.Position
			clone.ParticleEmitter.Enabled = true
			clone.Parent = workspace

			local weld = Instance.new('Weld')
			weld.Part0 = clone
			weld.Part1 = root
			weld.Parent = clone

			on = true
		end
	end
end)

1 comment

0

Thanks!

I've re-written your script so it should only fire once when you press 'Z'

--//Services
local UIS = game:GetService('UserInputService')
local serverStorage = game:GetService('ServerStorage')
local replicatedStorage = game:GetService('ReplicatedStorage')

--//Variables
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild('Humanoid')
local root = char:WaitForChild('HumanoidRootPart')
local debounce = true
UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Z and debounce then
	debounce = false
        local clone = replicatedStorage:WaitForChild('Particle'):Clone()
        clone.Parent = game.Workspace
        clone.Position = root.Position
        clone.ParticleEmitter.Enabled = true

        local weld = Instance.new('Weld',clone)
        weld.Part0 = clone
        weld.Part1 = root
     
    if clone.ParticleEmitter.Enabled == true then
        if input.KeyCode == Enum.KeyCode.Z then
            clone.ParticleEmitter.Enabled = false
        end
        end
    end
end)

UIS.InputEnded:Connect(function(input)
	debounce = true
end)

Hope this helps!

1 comment

0

It didn't make a difference sry

This discussion is preserved as it was. New answers, comments, and votes are closed.