Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Sorukan 240 Moderation Voter
5 years ago
Edited 5 years ago

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)
0
try adding a wait Theroofypidgeot 21 — 5y
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. Rheines 661 — 5y

2 answers

Log in to vote
1
Answered by
Rheines 661 Moderation Voter
5 years ago
Edited 5 years ago

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)
0
Thanks! Sorukan 240 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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!

0
It didn't make a difference sry Sorukan 240 — 5y

Answer this question