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

Hold Down SpotLight?

I made a Script for when you push Ctrl, it Inserts a PointLight into your Torso. The only problem is, you can spam Insert it, so you get a solid light, which isn't the best for my game. Can anyone make it so when you hold it, the PointLight stays on until then?

mouse = game.Players.LocalPlayer:GetMouse()

mouse.KeyDown:connect(function(key)
	if string.byte(key) == 50 then
		L = Instance.new("SpotLight")
		L.Brightness = 8
		L.Range = 25
		wait()
		L.Parent = game.Players.LocalPlayer.Character["Torso"]
		wait(2)
		L:remove()
		script.Disabled = true
		wait(5)
		script.Disabled = false
	end
end)

1 answer

✓ Accepted answer

I can see you understand how KeyDown works, but there is an Event called KeyUp, which fires when a key is released.

With that being said, you can create the PointLight when you press a key (KeyDown) and remove it when you release a key (KeyUp). Or you can just create it when a player spawns, and just change it's Enabled property for efficiency.

Check this out:

mouse = game.Players.LocalPlayer:GetMouse()

repeat wait() until game.Players.LocalPlayer.Character["Torso"]

L = Instance.new("SpotLight", game.Players.LocalPlayer.Character["Torso"])
L.Brightness = 8
L.Range = 25
L.Enabled = false

mouse.KeyDown:connect(function(key)
	if string.byte(key) == 50 then
		L.Enabled = true
	end
end)

mouse.KeyUp:connect(function(key) --Note the event named "KeyUp".
	if string.byte(key) == 50 then
		L.Enabled = false
	end
end)

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