Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i make Keybinds that change part material using this script???

Asked by 6 years ago
Edited 6 years ago

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)

0
this is one of those questions where you haven't supplied use with much information and your question isn't very specific, please elaborate on what you want help with. Just giving us a script and asking us to do something is making us do it for you, you need to give us your attempt then we will help. SynthetickDev 188 — 6y
0
use a code block hellmatic 1523 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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!

Ad

Answer this question