Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Flashlight in a gun?

So i have a script for a flashlight you can use it whenever you want i wanna make it only usable on a equipped gun that i have can someone help me. This is the script:

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 
 
       
        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" then 
    Light()
end
end)

1 answer

✓ Accepted answer
local tool = game.Workspace.Gun
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.Parent == plr.Character then 
    Light()
end
end)



2 comments

0

When i unequipped the gun the light still stays how can i make it disappear

0

Make an event for the tool. tool.Unequipped:Connect(function() not sure what the unequipped function is called.

This discussion is preserved as it was. New answers, comments, and votes are closed.