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

Power Drainage Part 2?

Asked by 9 years ago

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)

2 answers

Log in to vote
0
Answered by 9 years ago

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)
Ad
Log in to vote
0
Answered by
Discern 1007 Moderation Voter
9 years ago

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. :)

0
I tried yours (And Turbo's) but it didn't work in the game? ScriptingHelpersALT 20 — 9y
0
Try 'repeat wait() until game.Players.LocalPlayer' in line one Redbullusa 1580 — 9y
0
It worked for me. Make sure this is in a LocalScript and that it's in StarterPack or StarterGui. Discern 1007 — 9y

Answer this question