Flash light script, randomly after a couple rounds in my game. Instead of just pressing F to activate, it requires “Shift” + F
Any ideas?
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
-- Find GUI
local playerGui = player:WaitForChild("PlayerGui")
local flashlightGui = playerGui:WaitForChild("Flashlight")
local frame = flashlightGui:WaitForChild("Frame")
local button = frame:WaitForChild("ImageButton")
local guiScript = flashlightGui:WaitForChild("Script")
local sound = guiScript:WaitForChild("FlashLightSound")
-- Flashlight data
local flashlightAttachment = nil
local flashlight = nil
local connection = nil
local flashlightOn = false
local FIRST_PERSON_THRESHOLD = 0.9
local OFFSET_DISTANCE = 2
-- ======= FLASHLIGHT TOGGLE FUNCTION =======
local function SetFlashlightState(state)
if flashlightOn == state then return end
flashlightOn = state
-- Visual indicator color
if flashlightOn then
frame.BorderColor3 = Color3.new(0, 1, 0)
else
frame.BorderColor3 = Color3.new(1, 0, 0)
end
sound:Play()
if flashlightOn then
local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
-- Create attachment and light
local attachment = Instance.new("Attachment")
attachment.Name = "FlashlightAttachment"
attachment.Parent = Workspace.CurrentCamera
local light = Instance.new("SpotLight")
light.Name = "Flashlight"
light.Parent = attachment
light.Range = 60
light.Angle = 80
light.Shadows = true
light.Brightness = 3
flashlightAttachment = attachment
flashlight = light
connection = RunService.RenderStepped:Connect(function()
if not flashlightAttachment or not Workspace.CurrentCamera then
if connection then connection:Disconnect() end
return
end
local cam = Workspace.CurrentCamera
local head = player.Character and player.Character:FindFirstChild("Head")
if not head then return end
local camPos = cam.CFrame.Position
local camLook = cam.CFrame.LookVector
local lookDirection
-- Determine if first or third person
local camDistance = (camPos - head.Position).Magnitude
if camDistance <= FIRST_PERSON_THRESHOLD then
lookDirection = camLook
else
local ray = Workspace:Raycast(camPos, (mouse.Hit.Position - camPos).Unit * 500)
if ray then
lookDirection = (ray.Position - camPos).Unit
else
lookDirection = (mouse.Hit.Position - camPos).Unit
end
end
-- Position slightly in front of camera
local lightPos = camPos + lookDirection * OFFSET_DISTANCE
local lookCF = CFrame.lookAt(lightPos, lightPos + lookDirection)
local localCF = cam.CFrame:ToObjectSpace(lookCF)
flashlightAttachment.CFrame = localCF
end)
else
-- Turn off flashlight
if connection then
connection:Disconnect()
connection = nil
end
if flashlightAttachment then
flashlightAttachment:Destroy()
flashlightAttachment = nil
end
flashlight = nil
end
end
-- ======= INPUT CONNECTIONS =======
mouse.KeyDown:Connect(function(key)
if key:lower() == "f" then
SetFlashlightState(not flashlightOn)
end
end)
button.MouseButton1Down:Connect(function()
SetFlashlightState(not flashlightOn)
end)
-- Initialize GUI color
frame.BorderColor3 = Color3.new(1, 0, 0)