The operation of the flashlight is that when the player presses the F key, it is activated.
The thing is that when I press Esc, the flashlight is activated and I don’t know very well why.
local character = game.Players.LocalPlayer.Character
local playerInput = game:GetService("UserInputService")
local MobileService = game:GetService("ContextActionService")
local sound = script:WaitForChild("Toggle")
local linterna = Instance.new("SpotLight")
linterna.Angle = 80
linterna.Brightness = 7
linterna.Range = 24
linterna.Shadows = true
linterna.Enabled = false
local torso = character:GetChildren("Torso")
if torso then
linterna.Parent = character:WaitForChild("Torso")
end
local linternaOn = false
local function FlashLight()
if linternaOn == false then
linternaOn = true
sound:Play()
linterna.Enabled = not linterna.Enabled
MobileService:UnbindAction("FlashLight")
else
linternaOn = false
end
end
playerInput.InputBegan:Connect(function(input, gameprocess)
if (not gameprocess) then
if input.KeyCode == Enum.KeyCode.F then
sound:Play()
linterna.Enabled = not linterna.Enabled
end
end
end)
MobileService:BindAction("Flash light", FlashLight, true, Enum.KeyCode.F, Enum.KeyCode.ButtonB)
MobileService:SetPosition("Flash Light", UDim2.new(1, -30, 1, 0))
I’m not entirely sure why but you can do some things to your script to help and may fix the issue maybe Roblox is just being weird
Remove the playerInput function and just add that code into the FlashLight() function
I haven’t used BindAction in a while but what’s the Enum.KeyCode.ButtonB? could that be why?
You also spelled “Flash Light” 3 different ways in BindAction/SetPosition/Unbind
edit: looking at your script again I think you can just remove the playerInput function entirety you’re doing what it’s doing in the FlashLight script already, also you’re Unbinding it so it’s not going to work if you try disabling/activating the flashlight again
local character = game.Players.LocalPlayer.Character
local playerInput = game:GetService("UserInputService")
local sound = script:WaitForChild("Toggle")
local linterna = Instance.new("SpotLight")
linterna.Angle = 80
linterna.Brightness = 7
linterna.Range = 24
linterna.Shadows = true
linterna.Enabled = false
local torso = character:GetChildren("Torso")
if torso then
linterna.Parent = character:WaitForChild("Torso")
end
local linternaOn = false
playerInput.InputBegan:Connect(function(input, gameprocess)
if (not gameprocess) then
if input.KeyCode == Enum.KeyCode.F then
sound:Play()
linterna.Enabled = not linterna.Enabled
end
end
end)
With this code the Esc problem was fixed, but i wanna add adroid buttons to my game.