SpotLight Disabling When Moving

Hello!

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? spotlight disabling when moved in other directions

  3. What Did You Try So Far? disabling shadows, rewriting the script a little bit

local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local FlashLight = RS.Assets.FlashLight:Clone()
local LP = game.Players.LocalPlayer

local Weld = Instance.new("Weld")
local Chr = LP.Character or LP.CharacterAdded:Wait()

Weld.Parent = Chr.Head
FlashLight.Parent = Chr.Head
Weld.Part0 = Chr.Head.FlashLight
Weld.Part1 = Chr.Head


print("Light Succesfully Attached On Head.")


UIS.InputBegan:Connect(function(inputObject, process)
	if inputObject.KeyCode == Enum.KeyCode.F then
		if process == true then return end
		FlashLight.SpotLight.Enabled = true
	else
		FlashLight.SpotLight.Enabled = false
	end
end)

I Am still learning luau :smiley:

1 Like

There’s a small logic error in your InputBegan event handler. You should only disable the spotlight if the F key is pressed AND the spotlight is currently enabled. Otherwise, it should remain enabled.

Try something like this:

UIS.InputBegan:Connect(function(inputObject, process)
    if inputObject.KeyCode == Enum.KeyCode.F then
        if process then
            return
        end
        FlashLight.SpotLight.Enabled = not FlashLight.SpotLight.Enabled
    end
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.