i looked at an article of inputservice and read some dev forum tutorials but after using the code suggested it didnt work.
im trying to make a flashlight connected to a part which the players camera is located in. im going to do this by making a debounce and setting the debounce to the opposite so when the key is pressed the script plays and disables/enables the flashlight. i also want a sound to play
the code:
local UserInputService = game:GetService("UserInputService")
local debounce = false
UserInputService.InputBegan:Connect(function(InputObject, Processed)
if InputObject.KeyCode == Enum.KeyCode.E then
if debounce == false then
script.Parent["Flashlight Click"]:Play()
wait(0.5)
script.Parent.Enabled = false
elseif debounce == true then
script.Parent.Enabled = true
script.Parent["Flashlight Click"]:Play()
wait(0.5)
debounce = false
end
end
end)
Hi. I assume that the camera part is located in the workspace.
LocalScripts will not run in the workspace. LocalScripts must be a descendant of the player(Player or Character) in order to run. If the camera part is located in the workspace, update the location of the local script and change the references in the local script.
So, First of all the logic for your entire code if off, so lets see it like this in the beginning the “debounce” is false so then when the user presses ‘E’ it will fire but the Spot Light will get disabled.
local UserInputService = game:GetService("UserInputService")
local debounce = false
UserInputService.InputBegan:Connect(function(InputObject, Processed)
if InputObject.KeyCode == Enum.KeyCode.E then
if debounce == true then
script.Parent["Flashlight Click"]:Play()
wait(0.5)
script.Parent.Enabled = false
debounce = false
elseif debounce == false then
script.Parent.Enabled = true
script.Parent["Flashlight Click"]:Play()
wait(0.5)
debounce = true
end
end
end)
Im assuming that Camera Part is a child of Camera, so it will work.
instead of using debounce you can use something similar to debounce its easier and better in my opinion
local UserInputService = game:GetService("UserInputService")
FlashEnable = false
UserInputService.InputBegan:Connect(function(InputObject, Processed)
if InputObject.KeyCode == Enum.KeyCode.E then
FlashEnable = not FlashEnable
if FlashEnable then
-- Open the flashlight, play sound etc
else
-- close flashlight stop sounds etc
end
end)