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

Making a Flashlight on a Gun

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

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!

5 comments

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)?

0

Copy this into the gun script.

0

Adding this into the gun script and modifying it so the path to the light is correct does not seem to work.

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.

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."

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);

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