This is the final thing I need for the Power Drainage, here is the full script. If I wanted the Power to go down once every 3 Seconds, only while holding down Ctrl, how would I do that, since it keeps going down when I let go?
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 while wait(3) do game.Lighting.PValue.Value = game.Lighting.PValue.Value - 1 end end end) mouse.KeyUp:connect(function(key) if string.byte(key) == 50 then L.Enabled = false end end)
It keeps going down when you let go of the CTRL key because you have nothing that breaks the loop that lowers power. In order to break the loop, you have to have a break
command in there. Like so:
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 while true do if (not L.Enabled) then break --This breaks the loop if the Light is disabled end game.Lighting.PValue.Value = game.Lighting.PValue.Value - 1 wait(3) end end end) mouse.KeyUp:connect(function(key) if string.byte(key) == 50 then L.Enabled = false end end)
While
loops would be very ineffecient for this. I would use a repeat
loop for this. You could make a boolean variable that checks if the key is held down or not, and then at the conditional statement, you could put if the boolean is true or not.
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 keyHeld = false --This would be the variable to check if the key is held down or not. mouse.KeyDown:connect(function(key) if string.byte(key) == 50 then L.Enabled = true keyHeld = true --Sets keyHeld to true. repeat game.Lighting.PValue.Value = game.Lighting.PValue.Value - 1 wait(3) until keyHeld == false --The loop will repeat itself until keyHeld is false. end end) mouse.KeyUp:connect(function(key) if string.byte(key) == 50 then L.Enabled = false keyHeld = false --Sets keyHeld to false. end end)
Hope I helped. :)