Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't this flashlight toggle script work?

Asked by
Paradoa 17
4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

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. xInfinityBear 1777 — 4y
0
Ok. thanks for the help. Dovydas1118 1495 — 4y
0
it says player is not part of DataModel Paradoa 17 — 4y
0
and the player for player.LocalPlayerCharacter and player.CharacterAdded is underlined red. Paradoa 17 — 4y
0
DataModel just means game. and if you're using a server script, then you CANNOT use LocalPlayer. Dovydas1118 1495 — 4y
Ad
Log in to vote
0
Answered by
Paradoa 17
4 years ago
Edited 4 years ago

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)

Answer this question