I am trying to get a tool to do a function when I press the E key and when I press it a second time it undoes the first function. Its for a lightsaber that I am making for fun, I can get the E key to activate it, but I do not know how to turn it off by pressing the same key again. I’m kind of new to scripting so sorry if I am just dumb.
This is a basic system:
local UIS = game:GetService("UserInputService")
local toggle = false -- when false, lightsaber off. true = on
UIS.InputBegan:Connect(function(input, gpe)
if not gpe and input.UserInputType = Enum.UserInputType.Keyboard then
if input.KeyCode = Enum.KeyCode.E then
toggle = not toggle -- turns on if off, off if on
end
end
end)
Then have a function for your lightsaber structured like so:
local function lightsaber(toggle)
if toggle == true then
-- turn it on
else
-- turn it off
end
end)
The script won’t run, I don’t seem to be getting any errors. I used the exact same script except I added an =
when checking if something was equal because you only had one of them. Even after fixing that it doesn’t seem to work.
Where are you storing this code in? In a LocalScript or a Script?
If it’s a LocalScript, then good. Otherwise, make sure that’s the case.
Where is the LocalScript parented to? StarterGui and StarterPlayerScripts are good options
It is a local script and its parented to StarterPlayerScripts.
Here’s a Basic System:
--// Services
local UserInputService = game:GetService("UserInputService")
--// Variables
local Toggled = Instance.new("BoolValue")
Toggled.Value = false
Toggled.Name = "Toggled"
--// Functions
local function ChangeStatus(Input, GameProcessedEvent)
if GameProcessedEvent then return end
if Input.KeyCode == Enum.KeyCode.E then
Toggled.Value = not Toggled.Value
end
end
local function ToggledChanged()
if Toggled.Value == true then
print("Enabled")
else
print("Disabled")
end
end
--// Events
UserInputService.InputBegan:Connect(ChangeStatus)
Toggled.Changed:Connect(ToggledChanged)
Your function lightsaber()
isn’t being called anywhere which is why you’re not getting any output.
toggle = not toggle
lightsaber(toggle)
Note that you’ll need to move the lightsaber()
function to above the UIS.InputBegan
line or else it won’t be defined.
Personally, I’d rid of the lightsaber()
function entirely and just do it all in the event if it were me but it’s up to you.
Here’s a version without the seperate ToggledChanged/lightsaber function like what Vulkarin recommended.
--// Services
local UserInputService = game:GetService("UserInputService")
--// Variables
local Toggled = false
local Debounce = os.clock()
--// Functions
local function ChangeStatus(Input, GameProcessedEvent)
if GameProcessedEvent or (os.clock() - Debounce) < 1 then return end
if Input.KeyCode == Enum.KeyCode.E then
if Toggled == false then
print("Enabled")
Toggled = true
else
print("Disabled")
Toggled = false
end
Debounce = os.clock()
end
end
--// Events
UserInputService.InputBegan:Connect(ChangeStatus)
I used this one and it works, thanks for the help.