Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Making a Flashlight on a Gun

Asked by
Corogl 0
10 years ago

Hey guys, I was wondering how do you make it so a certain light on a gun (script.Parent.Light.Light1) is enabled when you hit f, and disabled when you hit it again?

Also is it easy to just copy it if I wanted to make 2 lights in a gun?

2 answers

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

You can copy this script, just change the key (Obviously...)

Equipped = false
on = false
light = script.Parent.PointLight
script.Parent.Equipped:connect(function(Equipped)
    Equipped = true
end)
mouse.KeyDown:connect(function(key)
    local key = key:lower()
    if Equipped and key == "f" and on == false then
            light.Enabled = true
            on = true
    elseif Equipped and key =="f" and on == true then
        light.Enabled = false
        on = false

    end
end)

--[[script.Parent.Unequipped:connect(function()
    light.Enabled = false
end)--]]

--The above is if you want it to auto-set the light off if they put away the tool

-Thank me by accepting this answer/bumping up my reputation!

0
Does this script go inside the brick with the light, or do I copy it into the script that controls the gun's functions (so it's all one script)? Corogl 0 — 10y
0
Copy this into the gun script. Shawnyg 4330 — 10y
0
Adding this into the gun script and modifying it so the path to the light is correct does not seem to work. Corogl 0 — 10y
0
On the connection line, is the path correct? I just put script.Parent because I assumed that the script was a direct child of the tool. Shawnyg 4330 — 10y
0
The script IS a direct child, but you have that the pointlight is a part of the tool, which it isn't. It's in a block that's in the tool. I changed it to script.Parent.Light.Light1, with the name of the block being "Light" and the name of the light being "Light1." Corogl 0 — 10y
Ad
Log in to vote
0
Answered by 10 years ago
mouse=Game.Players.LocalPlayer:GetMouse();
light=WHERE_TO_FIND_THE_LIGHT;
mouse.KeyDown:connect(function(key)
if key:lower()=='f' then
light.Enabled=not light.Enabled;
end;
end);

Answer this question