How do I activate a flashlight when I press a key?
local m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:connect(function(k)
k = k:lower()
if k == "f" then
game.StarterPlayer.StarterCharacter.Flashlight.SpotLight.Brightness = 1
end
end)
How do I activate a flashlight when I press a key?
local m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:connect(function(k)
k = k:lower()
if k == "f" then
game.StarterPlayer.StarterCharacter.Flashlight.SpotLight.Brightness = 1
end
end)
The central issue behind why your script is malfunctioning, is a semantic error. StarterCharacter is a replication container, which is only designed to transfer it's content into the loading Character; the changes you've made to the flashlight would only be replicated upon respawn.
To resolve this problem, you have to reference the active tool, which can be found in the Character when equipped. This way, modifications will be appended properly.
####Note:
Mouse, though still containing some key functionalities, has been superseded by UserInputService. If you were to research the KeyDown signal, you'd immediately be met by a deprecation notice. UserInputService should be used in all further practices pertaining user-Input management.
What does it mean to have a deprecated method? This means that the method is no longer supported by the ROBLOX Engine. As the API and systems involved progress in development, the predessors may become faulty, and which is why it is recommended you do not use them. This way we avoid possible errors from rising, or improper functionality.
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
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 = Backpack:FindFirstChild("Flashlight")
if (FlashLight) then
Flashlight.SpotLight.Brightness = 1
end
end
end)
The InputBegan signal is used to attach our appropriate listener for Input related events, such as pressing a key. Not much is different with how we apply this code from .KeyDown, except that we have a particular debounce (it is still recommend by that we continue to use this Service, regardless of its similarities). This is a Boolean named GameProcessed which defines if ROBLOX is internally processing the same input, and to give priority to the system. Essentially, we tell our code to not run the KeyBind we've made, in order to prevent a potential mess when this scenario occurs. An example of this would be making a message in Chat, this way, we ensure we don't keep toggling the flashlight each time we press E while typing.
Mouse KeyDown is only for mouse buttons. You have to use UserInputService and use InputBegan.
Additionally you have 2 more mistakes. First off you are using StarterCharacter to edit (which is not gonna do anything until you respawn). And secondly you have no remote event or whatever setup so only the localplayer will see the flashlight.
This discussion is preserved as it was. New answers, comments, and votes are closed.