How would i make a toggle button using UserInputService?

I want the script to invert colors whenever i press Q, and i want it to revert to normal when i press it again

Theres no issue with the script itself i just dont know how to go about it and i’d like some help. I started scripting not too long ago and i cant seem to figure it out

Ive tried using Boolean values, like how people script opening and closing doors on click, but it didnt work

local UIS = game:GetService("UserInputService")
local lighting = game.Lighting
local bool = false
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		bool = true
		
		repeat
			wait(1)
			lighting.ColorCorrection.Saturation = -3
			print("Inverted Colors!")
		until bool == false
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		
		wait(1)
		lighting.ColorCorrection.Saturation = 0
		print("Normal Colors")
		bool = false
		end
end)

If its possible and someone has the answer please go into detail about how exactly is it done. Im still learning about scripting and i’d like an explanation i learn from

Thank you for your time

1 Like

I would do something like this instead (haven’t tested this code). It waits for an input to start and checks so the key is correct, after that it checks what the lightState is and if it’s true it will modify the light. After that is will take the reverse of the bool value so if it’s true it will be false and same the other way around.

local UIS = game:GetService("UserInputService")
local lighting = game.Lighting
local lightState = false

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		if lightState then
            lighting.ColorCorrection.Saturation = -3
        else
            lighting.ColorCorrection.Saturation = 0
        end	

        lightState = not lightState
    end
end)

A tip for the future: Try to avoid wait() as much as you can since it halts the execution of the thread.

2 Likes

Thank you so much! the script worked perfectly! But i dont understand why we invert the LightState Value?

Sorry for responding so late!

1 Like