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

Hold Down SpotLight?

Asked by 10 years ago

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

Log in to vote
3
Answered by
Discern 1007 Moderation Voter
10 years ago

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

Answer this question