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)
if you want it to only activate when equipped, you can easily set a bool value for when the tool is equipped or not. Also, don't use the parent parameter of Instance.new(). It's old, slower, and deprecated. Also, I recommend UserInputService instead of KeyDown. If this script, it may be because the tool is in ReplicatedStorage and not actually in the player's backpack, so just be aware of that.
local tool = game.ReplicatedStorage.M4A41 local plr = game.Players.LocalPlayer local mouse = game.Players.LocalPlayer:GetMouse() local equipped = false function Light() player = game.Players.LocalPlayer playerChar = player.Character playerLight = playerChar.Torso:FindFirstChild("Light") if playerLight then playerLight:Destroy() else light = Instance.new("SurfaceLight") light.Name = "Light" light.Range = 15 light.Brightness = 5 light.Shadows = true light.Enabled = true --Its something like this light.Parent = playerChar:WaitForChild("Torso") or playerChar:WaitForChild("HumanoidRootPart") local play = Instance.new("Sound") play.SoundId = "http://www.roblox.com/asset/?id=198914875" play.Name = "LightSound" play.Parent = playerChar:WaitForChild("Head") play:Play() end end tool.Unequipped:Connect(function() equipped = false if playerChar:WaitForChild("Torso"):FindFirstChild("Light") then playerChar:WaitForChild("Torso"):FindFirstChild("Light"):Destroy() elseif playerChar:WaitForChild("HumanoidRootPart"):FindFirstChild("Light") then playerChar:WaitForChild("HumanoidRootPart"):FindFirstChild("Light"):Destroy() end if playerChar:WaitForChild("Head"):FindFirstChild("LightSound") then playerChar:WaitForChild("Head"):FindFirstChild("LightSound"):Destroy() end end) tool.Equipped:Connect(function() equipped = true end) game:GetService("UserInputService").InputBegan:Connect(function(input,gameProcessed) if gameProcessed or equipped == false then return end if input.KeyCode == Enum.KeyCode.L then Light() end end)