So i have this flash light script its inside the gun inside Replicated Storage and then i have a pick up system where it clones it from Replicated Storage The light should ONLY activate when i press l when my gun is equipped and disappear if i unequip , right now i can press l and it will activate just by staying in my inventory(not equipped) and it doesent disappear if i unequip the gun.
local tool = game.ReplicatedStorage.M4A41
local plr = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()
function Light()
player = game.Players.LocalPlayer
playerChar = player.Character
playerLight = playerChar.Torso:FindFirstChild("Light")
if playerLight then
playerLight:Destroy()
else
light = Instance.new("SurfaceLight",playerChar:FindFirstChild("Torso"))
light.Name = "Light"
light.Range = 15
light.Brightness = 5
light.Shadows = true
light.Enabled = true --Its something like this
local play = Instance.new("Sound",playerChar:FindFirstChild("Head"))
play.SoundId = "http://www.roblox.com/asset/?id=198914875"
play:Play()
end
end
mouse.KeyDown:connect(function(key)
key = key:lower()
if key == "l" and tool.Equipped then
Light()
tool.Unequipped:Connect(function()
playerChar.Torso.Light:Destroy()
end)
end
end)

3 comments
I tried the code and its not working. Its a local script inside the gun inside Replicated Storage should it be somewhere else?
I've tried putting it in ServerScriptService, Workspace , StarterCharScripts , StarterPlayerScript and it doesent seem to work any idea why could there something be wrong with the code?
the gun is a tool, and it should be in the player's backpack. the script should go inside the gun with the tool variable as script.Parent