Need help with an enable script

I have a base layout for a particle effect that should be Enabled when I’m holding down a key and should be disabled right after I let go of the key.

However, it isn’t working, and I can’t figure out why.

Here’s the current script:

local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Fountain = game.Workspace.FountainC.cannon.particleCore.fountain
local UpdateCon

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.C then return end

	if UpdateCon then
		UpdateCon:Disconnect()
		UpdateCon = nil
	end

	UpdateCon = RunService.Heartbeat:Connect(function(dt)
		Fountain.Enabled = true
	end)
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.C then return end
	if not UpdateCon then return end

	UpdateCon:Disconnect()
	UpdateCon = nil
end)

The “fountain” at the end of the “local Fountain” part is a ParticleEmitter. I’m not sure how to get a particle emitter to enable and disable with a script.

Any help is appreciated! :slight_smile:

Hey there, you can use this script:

local userInputService = game:GetService("UserInputService")
local particle = game.Workspace.FountainC.cannon.particleCore.fountain

userInputService.InputBegan:Connect(function(input, gp)
	if gp then return end

    if input.KeyCode == Enum.KeyCode.C then
        particle.Enabled = true
    end
end)

userInputService.InputEnded:Connect(function(input, gp)
	if gp then return end

    if input.KeyCode == Enum.KeyCode.C then
        particle.Enabled = false
    end
end)

This doesn’t seem to be working.

I forgot to mention the script is a LocalScript, located in StarterGui.

Should this be changed in any way?

Are you receiving any errors in the output (I assume not)? Also, what exactly do you mean it isn’t working. Do you mean the ParticleEmitter doesn’t get enabled at all? Have you checked to see if the code even runs (by print statements)?

Apparently, it didn’t work if I used the “Run” mode on the studio. Pressing Play instead made it work.

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