LightKey = "z" Light = workspace.CrowdLight.SurfaceLight
LocalPlayer = game.Players.LocalPlayer Mouse = LocalPlayer:GetMouse() On = false if Light.Enabled == true then On = true end
Mouse.KeyDown:connect(function (key) if key == LightKey then if On == true then Light.Enabled = false On = false elseif On == false then Light.Enabled = true On = true end end end)
Make a LocalScript, and then use ContextActionService
Here is an example:
function DoThing(action,state,obj) if(state == Enum.UserInputState.Begin) then --do stuff end end local contextService = game:GetService("ContextActionService") contextService:BindAction("ChangeMaterial,DoThing,false,Enum.KeyCode.Z)
As you can see, there are a couple of things going on here:
Firstly, we would need to bind the action of someone pressing "Z" on their keyboard, to do this, we're going to use :BindAction() as written in this code block. :BindAction() however, has 4 parameters.
The first parameter is actionName, this is the actual name of this action. This is mostly used if you have the same function but want different keys to trigger it. I will explain that later.
The second parameter is a function, or functionToBind. This is what will actually happen when someone triggers this event. The function specified will have 3 parameters you can use: action, state, and obj.
action is the name of the action. As said before, this can be used if you want to have 1 function work for many different keybinds.
state is the input state, which will have 4 values. You can learn more information about these here.
obj is the sender. I don't know much about this parameter, so I don't think you will need it most of the time.
Now, you can see that in the function, I included if(state == Enum.UserInputState.Begin)
. This means that this code will only run if the user began to press "Z" on their keyboard. After putting down this line of code, you can now write whatever you want to happen when a user presses "Z"
The third parameter is createTouchButton. This is mainly used for mobile devices. Since they have no keyboard to work with, if this value is set to "true", then ROBLOX will create a GUI element which will trigger that event also.
Finally, the last parameter is inputTypes. Usually, this is set as an enum value called "KeyCode", which represent every single key on a computer keyboard.
I hope this helped!