Flashlight keybind script not working

Hi all
I am making a horror game and i was making a flashlight keybind but the script for that wont work!

Here’s the script

local mouse = game.Players.LocalPlayer:GetMouse()

function Light()
	local player = game.Players.LocalPlayer
	local playerChar = player.Character
	local playerLight = playerChar.Head:FindFirstChild("Light")

	if playerLight then
		playerLight:Destroy()
	else
		local light = Instance.new("SurfaceLight",playerChar:FindFirstChild("Head"))
		light.Name = "Light"
		light.Range = 60
		light.Angle = 50
		light.Shadows = true
		light.Brigtness = 15.6
	end
end

mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key == "f" then
		script.Sound:Play()
		Light()
	end
end)

use UserInputService.

local uis = game:GetService(“UserInputService”)

uis.InputBegan:Connect(function(k)
if k.KeyCode == Enum.KeyCode.F then
– your script goes here
end
end)

First of all, you SHOULDN’T be using <Mouse>.KeyDown to handle player input, there’s better and more developed options like UserInputService or ContextActionService. For this example I’d rather say to do this:

local keyCode = Enum.KeyCode.F
local userInputService = game:GetService("UserInputService")
userInputService.InputBegan:Connect(function(_input, gameProcess)
    if gameProcess then return end
    if _input.KeyCode == keyCode then
        flashLight()
    end
end)

This is how it should be looking (I didn’t want it to be highlighted with the input keyword, so I put an underscore before it). From there you can modify the flashlight function to actually work with a control variable (so people can turn it off and on).

1 Like

It is actually userInputService. TouchStarted
Not touchBegan

My bad in the writing, I meant InputBegan.

what part dosent work? as the input works

Your light script is client side, are you sure that you want it? No one will see when your light is on. And i recommend you use UserInputService instead player:GetMouse.

I would recommend using UserInputService instead.

local UIS = game:GetService("UserInputService")

https://developer.roblox.com/en-us/api-reference/class/UserInputService

First Create A RemoteEvent In ReplicatedStorage:
image

Second, Create a LocalScript in StarterPlayerScripts:

LocalScript:

local UIS = game:GetService("UserInputService")

local ReplicatedStorage = game.ReplicatedStorage
local Input = ReplicatedStorage.Input

UIS.InputBegan:Connect(function(input, _)
	if input.KeyCode == Enum.KeyCode.E then
		return Input:FireServer(true) -- Pressing Status
	end
end)

UIS.InputEnded:Connect(function(input, _)
	if input.KeyCode == Enum.KeyCode.E then
		return Input:FireServer(false) -- Pressing Status
	end
end)

Third, Create A ServerScript in ServerScriptService or Inside The Tool and insert the sound inside the script, The following one will be in the serverscriptservice:

ServerScript:

local ReplicatedStorage = game.ReplicatedStorage
local Input = ReplicatedStorage.Input

function Light(player)
	local playerChar = player.Character
	local playerLight = playerChar.Head:FindFirstChild("Light")

	if playerLight then
		playerLight:Destroy()
	else
		local light = Instance.new("SurfaceLight",playerChar:FindFirstChild("Head"))
		light.Name = "Light"
		light.Range = 60
		light.Angle = 50
		light.Shadows = true
		light.Brigtness = 15.6
	end
end

Input.OnServerEvent:Connect(function(player, isPressed)
	if isPressed == true then
		script.Sound:Play()
		Light()
	end
end)

For more information about UserInputService:

https://developer.roblox.com/en-us/api-reference/class/UserInputService

Or via Devfourm:

I hope that i did help you.

Thanks,
Mezo.

1 Like