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

Putting to instructions into a button input?

How do you make it so that when you input a button it does something (I've covered that) but when you press it again it does something else?

For example, what i've tried is:

if key == "l" then
local part = player.Character.Torso
local lightsource = Instance.new("SurfaceLight")
lightsource.Parent = part
lightsource.Brightness = 2
	wait(2)
	player.PlayerGui.Light.TextLabel.TextTransparency = 0.1
	wait(.1)
	player.PlayerGui.Light.TextLabel.TextTransparency = 0.2
	wait(.1)
	player.PlayerGui.Light.TextLabel.TextTransparency = 0.3
	wait(.1)
	player.PlayerGui.Light.TextLabel.TextTransparency = 0.4
	wait(.1)
	player.PlayerGui.Light.TextLabel.TextTransparency = 0.5
	wait(.1)
	player.PlayerGui.Light.TextLabel.TextTransparency = 0.6
	wait(.1)
	player.PlayerGui.Light.TextLabel.TextTransparency = 0.7
	wait(.1)
	player.PlayerGui.Light.TextLabel.TextTransparency = 0.8
	wait(.1)
	player.PlayerGui.Light.TextLabel.TextTransparency = 0.9
	wait(.1)
	player.PlayerGui.Light:remove()
else
	local light = player.Character.Torso.SurfaceLight
	light:remove()
end
end)

I obviously know that 'else' won't work, but i dont know what is the alternate option.

Would be nice if i got some code explaining how it works, or just a brief explanation on what to do.

1 answer

✓ Accepted answer

You want to set a variable that you will toggle, and check which state it is in.

-- This variable will keep track of weither the button was pressed before or not
local toggle = true

if key == "l" then
  -- That variable is checked here
  if toggle then
    local part = player.Character.Torso
    local lightsource = Instance.new("SurfaceLight")
    lightsource.Parent = part
    lightsource.Brightness = 2
    wait(2)

    -- Make use of loops for this sort of usage
    for num=1,9 do
      player.PlayerGui.Light.TextLabel.TextTransparency = 0.1 * num
      wait(.1)
    end

    player.PlayerGui.Light:Destroy()
  else
    -- You should probably check if the light actually exists, before you attempt to delete it
    local light = player.Character.Torso:FindFirstChild("SurfaceLight")

    if light then
      light:Destroy()
    end
  end
  -- Changes the state of toggle to the oposite
  toggle = not toggle
end

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