My intention is for the flashlight to be toggled when the player presses the key "E". The flashlight is a part of the StarterCharacter body. Also, how do I make it toggle?
local UserInputService = game:GetService("UserInputService") UserInputService.InputBegan:Connect(function(InputObject, GameProcessed) if (GameProcessed) then return end if (InputObject.KeyCode == Enum.KeyCode.E) then local Character = Player.Character or Player.CharacterAdded:Wait() local Flashlight = Character.Flashlight if (Flashlight) then Flashlight.SpotLight.Brightness = 1 print("Pressed!") end end end)
Edit: I played around with the script a bit and it still doesn't work
local UserInputService = game:GetService("UserInputService") UserInputService.InputBegan:Connect(function(InputObject, GameProcessed) if (GameProcessed) then return end if (InputObject.KeyCode == Enum.KeyCode.E) then local character = game.Players.LocalPlayer.CharacterAdded:wait() or game.Players.LocalPlayer.Character local flashlight = character:WaitForChild("Flashlight") flashlight.SpotLight.Enabled = not flashlight.Spotlight.Enabled print("Yeet!") end end)
First of all, remove the brackets from(Gameprocessed) and everything else that doesn't the require the brackets.. (do not remove the () from built in functions.) Secondly, you do not need an if statement if you're just gonna toggle the flashlight on and off. Do not change the brightness, rather change the Visible property instead.
Flashlight.SpotLight.Enabled = not Flashlight.Spotlight.Enabled
It basically switches back and forth. If the visible is true, then it's false, and vice-versa.
Edit: My stupid grammar. Edit2: I changed some things.
Solved it!
local UserInputService = game:GetService("UserInputService") local player = game:GetService("Players") UserInputService.InputBegan:Connect(function(InputObject, GameProcessed) if (GameProcessed) then return end if (InputObject.KeyCode == Enum.KeyCode.E) then local character = player.LocalPlayer.Character local flashlight = character:findFirstChild("Flashlight") flashlight.SpotLight.Enabled = not flashlight.SpotLight.Enabled end end)