How to have the same key to turn something on and off?

Using ContextActionService I want to be able to have the Enum.KeyCode.r key to turn my radio on and off how do I do that I only know how to have it turn it on or off not both?

Check if it’s on with an if-statement and if it is then turn it off or vice-versa. Hope this helps.

If radio is on then
Turn radio off
Else
Turn radio on
End

@xDeltaXen this pseudocode will work.

1 Like

Dont dont really see how that can work

local UIS = game:GetService("UserInputService")
local radio = --sound
radio.Volume = 1

UIS.InputBegan:Connect(function(input)
       if input.KeyCode == Enum.KeyCode.R then
             if radio.Volume == 1 then
              radio.Volume = 0
              else
              radio.Volume = 1
              end
       end
end)
1 Like

You can do it even shorter than the replies by using a ternary operator

local uis = game:GetService("UserInputService")
local radio = -- reference to radio
local volume = 1 -- what the volume of the radio should be when turned on


uis.InputBegan:Connect(function(input, gp)
    if gp then return end 

    if input.KeyCode == Enum.KeyCode.R then 
        radio.Volume = (radio.Volume > 0) and 0 or volume
    end
end)

Sorry if this is badly formatted, wrote this on mobile.

1 Like