Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why won't this flashlight toggle script work?

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)

2 answers

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.

5 comments

0

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.

0

Ok. thanks for the help.

0

it says player is not part of DataModel

0

and the player for player.LocalPlayerCharacter and player.CharacterAdded is underlined red.

0

DataModel just means game. and if you're using a server script, then you CANNOT use LocalPlayer.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.