I have a script that should enable a particle emitter using the C key and should stop emitting when that key is released.
However, I’m not sure how to get that to work, and where the placement should be.
Here’s the script:
local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local particle = game.Workspace.FountainC.cannon.particleCore.fountain
local UpdateCon
userInputService.InputBegan:Connect(function(input, gp)
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(gp)
particle.Enabled = true
end)
end)
userInputService.InputEnded:Connect(function(input, gp)
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)
This script is a LocalScript, and is located in the StarterGui.
Why connecting into a RunService?, I tested it and works fine like this:
local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local particle = game.Workspace.FountainC.cannon.particleCore.fountain
local UpdateCon
userInputService.InputBegan:Connect(function(input, gp)
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(gp)
particle.Enabled = true
--end)
end)
userInputService.InputEnded:Connect(function(input, gp)
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
particle.Enabled = false
end)
Without the RunService connection values:
local userInputService = game:GetService("UserInputService")
local particle = game.Workspace.FountainC.fountain
userInputService.InputBegan:Connect(function(input, gp)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.C then return end
particle.Enabled = true
end)
userInputService.InputEnded:Connect(function(input, gp)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.C then return end
particle.Enabled = false
end)
local UserInputService = game:GetService("UserInputService");
local Held = false;
local Func = coroutine.create(function()
if held == true then
--Yourfunction
end
end)
UserInputService.InputBegan:Connect(function(Input)
if Input.UserInputType == Enum.KeyCode.C then
coroutine.resume(Func)
Held = true;
end
end)
UserInputService.InputEnded:Connect(function(Input)
if Input.UserInputType == Enum.KeyCode.C then
Held = false;
end
end)
coroutine.resume(Func);Preformatted text
1 Like