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)

5 comments
A spotlight doesn't have a 'Visible' property, use the Enabled property instead. Also things like (GameProcessed) with the parenthesis I see people use, I also use them myself sometimes to keep the script clean. It's personal preference though, it won't harm the code.
Ok. thanks for the help.
it says player is not part of DataModel
and the player for player.LocalPlayerCharacter and player.CharacterAdded is underlined red.
DataModel just means game. and if you're using a server script, then you CANNOT use LocalPlayer.