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

Power Drainage Part 2?

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

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

3 comments

0

I tried yours (And Turbo's) but it didn't work in the game?

0

Try 'repeat wait() until game.Players.LocalPlayer' in line one

0

It worked for me. Make sure this is in a LocalScript and that it's in StarterPack or StarterGui.

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