Advanced Roblox Scripting Tutorial #26 - ContextActionService (Beginner to Pro 2020)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right guys it is deafening here back with a brand new video and guys in today's video I'm gonna be teaching you about context action service now it's a pretty useful service but before I explain it any more let me go ahead and show you exactly what it's gonna do alright so I'm gonna start in the game and keep an eye on this little blue block right here okay if I go ahead and press C right now it turns yellow okay that's pretty cool I like it but basically what that means is like it it turns yellow right I mean and it's only a few lines of code so basically it's how you bind actions to two buttons and you can actually even use mobile emulators so like this like if I let sales on my phone right and this is an emulator I just went to test in the hit device and then I wanted like an iPhone 5 or an iPhone all down seven plus right and I started the game what's gonna happen is I'm actually gonna have a button now that I created customly and I actually put a little Pikachu icon for it but if I go ahead and press that button now it's gonna make up its make that green right or yellow so that's pretty cool I mean it's I like that but yeah anyways that's what that does okay so how do we do this you may be asking well let me go ahead and leave to the script and show you guys alright so let's go and go into a blank baseplate solid file new it all so some of you guys probably wondering like well why don't you just use user input service and I'm gonna explain that actually just in a second here that's when the difference between the two and why you should use one of the other but anyways let me go ahead and close the emulator for now I shall keep it open alright so the first step to actually creating a contact action service is making a local script because it's sort of like user input service so you need to actually make it sure it's a local script because you can't get input from a server script so we're gonna go ahead and make a local script okay and what we're gonna do now is get contact that's get context action service and to a variable so local context action service is equal to game get service contact action service there we go and also like I said what's the difference between user input service and this one or a context action service basically context action service is a game service that pretty much allows you to like user input to certain actions so you basically you're gonna be using a function called context action service bind action and basically it's how you bind actions to to a function so let me just actually show you the syntax and how it's gonna look and then I'll explain it all I guess all right so first things first we need to make a function to actually define what we want this you know a little guy to do so when you make a function right or we want our key to do so for us we're gonna make a little part right here actually and I'm going to give it a just me like that and what we want to do is we want to first of all obviously get the parts we'll do local part as equal to game that workspace top part and then we'll do part or notability local function on on button press ok and now we're adding this function right here okay and what we're gonna do is we're going to do part now brick color does he little brick color new and then we're gonna put in here new Yeller and it's pretty weird our brick color works like you can add a change with a color or a break color but for this we'll just use a brick color to keep it simple but basically you just do brick color new yellow because that's one of the colors as you can see here I've put it on there you can see it's new Yeller I know it's a weird name for a color but it is so yeah anyways now that we have like that we'll just see like there and now we have the actual function done right it's gonna work but now we can actually make a button so that way if someone presses the button or a key on the keyboard it will run this function so how do we do that well it's pretty simple it's one line of code all you have to do is do contact action service : bind action okay now as you can see here when I type this in : my - and I have quite a few parameters right here okay um less you wait I just me to go away first things first I have the string of the action name so we have to come up with a name for the for the action because if you ever want to access or deactivate it you need to have a name for it so we'll name it like on our turn turn button or turn brick yellow as well name it and then we'll do the function so what function do we want to bind it to obviously this one the on button press one okay and then what do we want to create a touch button on mobile and for this example we'll do it yes and then the input type so do you want to have it so or what yeah this is I'm pretty sure this is an acquired function we're gonna require but basically do you what do you want it to be for input on a computer so yeah anyways let's go ahead and do that now so I'll type that in and the first parameter like I said is the name so we're gonna name it turn brick yellow alright there we go and then we're going to put the function which is on button press and then we're going to remove the parentheses you're not supposed to add front this is when you pass through a function in here so just leave those parentheses it'll know it's a function anyways and then we're gonna go ahead and put the nor do we want it to make a button for this yes true and then do we want it to have a key to make it up you know like actually turn different colors so what key on your what key would it be on computer and for this one we'll do t okay makes sense so we're gonna put a t in there little low case T in quotes and then now if we run the function or run the game or should be in the emulator still I'm pretty sure and as you can see here we spawn and we have the normal mobile controls and there's actually a little bit there's a little button right here it's a tiny little button it's actually currently overlapping the jump button which we will fix in a second so don't worry but as you can see now I can run around I can do all this stuff right here alright pretty cool pretty cool and basically this is this is um you know just mobile controls but now if I press this little button right here the brick is now yellow and now obviously we can make it so it would switch back and forth if you want it to but currently we just made it do one thing which is turn it yellow and not turn it back so yeah but if we wanted to obviously you just write up all there to make it do that so yeah that's pretty cool all right so now if we go ahead and disable the emulator I can go ahead and actually start playing the game and what is what's gonna happen right now is I can just press C set off the button on my screen and look it's our mobile players I can press C and then if it would work it's oh no T sorry yeah not see rusty and boom it's still oh that's awesome so yeah you may be wondering okay so let me explain that actually why you would use context action servers rather than user input service so you might be wondering like okay well there's user input service where you can just do you know you could just do you user input service and then you know input begin and then look for T right you can do all that stuff if you wanted to also if you don't know how to use user input service who doesn't watch my video on that link should be in description but yeah anyways you may we might be wondering why you would use this one instead of or why you would use Haunted service instead of user input service and the reason as to why is is because sometimes on user input service you don't you don't always like want to have a button be doing something like for example okay if you had user input service set 40 okay so for the letter T so if it if it was like user input server's colon input began and then it you would do like if player push down the T key right or then you would want to do something but the thing is worth the players typing in chat and they're saying hey tell me how to play as you can see they typed in two T's there they say that's he for tell the T for dum - now what that would do actually is that would actually still count as a player pressing T if you didn't realize that it was still kind of present T so what you would want to do is you wouldn't want to like get confuse with that and you know get confuse from friend to say so yeah anyways also like okay I'm gonna be a good example of one you'd want to use it too a good example of what do you want to use it or when it's useful to use like if for example if you were getting into a car like a let's say there was a car wreck here let's say this is a car okay and you only want to get okay you want to get in the car right and you want to make it so when you're close to it there's an action that says II to enter right you don't always want to be looking for T because if you're always looking for T then you have to check the position of where it is see how close it is but what you could do is just by an action to it right and then you could just jump that way when you're close to it right or you're certain distance away from the car you just press key and then it'll get it and then you'll buy the action to it whenever it's close but then when it's far away again you can unbind the action so let me actually go ahead and show you what happens if I do that so if I go ahead and I'll put a weight like five and then I do a constant action service unbind action and I'll do turn brick yellow what's gonna happen now is I'm gonna unbind the action and it's not gonna show as sorry it's not kind of like you know it's not gonna be there anymore so let's go and test it out but we're gonna go on on mobile because we want you to see the button on on what's it called so we're gonna mobile and do it there we go and you can see right here it's going to unbind the action in five seconds so let me go to wait you can see the button right there and it's gone see and what if I've seconds are there unbind of the action so now if you press c RT nothing happens nothing at all it doesn't do anything because that the action is now unbind it so if you want to do something and you could rebind it that's what people do right you just rebind it and then you know you do that okay pretty pretty simple stuff I mean not very not very hard so yeah that's pretty much how that works it's pretty cool all this um how this works you don't use buying activate but for this well I'm not gonna make it too confusing all machine you used um this also you may be wondering how to change the like okay like if you had this little button right here right you have this button right here on your screen okay first of all it was touching the sorry it was touching the jump button right so how you actually move it is you do contacts action service set position and then you put in your parameters the first one the first one is a string name and the second one is the position so the will do turn break yellow that's the that's the name of the action and in the position you do a you dim to value you didn't knew and you do your scale offset Y scale and then Y offset but basically I'm gonna I'm not gonna do this right now because I actually have never taught you guys duis or yeah I never told you guys duis in my tutorials maybe you know about them but I've never explained on my tutorial so I don't want to confuse people who've never seen them before but basically you would just type in some numbers here right so would like 0.7 and then we'll do like I don't know like it it's there's I'm pretty sure I remembering right but there's a way to like get it above your certain character or if the jump button I forgot where that where it would be but I think that's it I don't know but as you can see here I'm gonna go on here and it's going to be in a different place and I actually don't did not type it right that's my bad um I've got my should parenthesis but it's I don't know where it's gonna be I didn't like do all the I didn't have like the place set but as you can see it moved a little bit but yeah it does how the position that's pretty cool but let me let me move it somewhere better I don't know point - there you go that that should be better I think I mean it's gonna be in a weird spot now I think it's really top here wait what okay that that's not working I'm not really too sure why I don't with you but anyways that's how you would do it maybe I am the game's telling me not to show you right now anyways other thing about this is you can also overwrite other actions so for example right here if I wanted to like it okay I'm gonna show you right now okay I'm gonna change this to D okay just to lowercase D and now if you if you realize when you move your character right you use WASD don't you I mean that's what that's what you use you don't use like the arrow keys you WASD so what's gonna happen is if I go ahead and actually bind that to D what's gonna happen is it's going to go ahead and not allow me to move on D wait actually I'm on a mobile so let me turn that off for a bit but whatever i press d now i'm not gonna move it's not gonna make me move at all this action is now overriding the the movement key so now if I press D actually wait I do move wait what this wasn't work nearly I'm so confused okay well anyways you see what I'm saying right but also a one-foot thing too if you press f9 and you go to up here to action bindings you can see all the actions that are binded right here you can see that D turn bright yellow action name that that's our action right here that we bind is right and you can also set priorities for your actions because if I were to like what should be happening right now is it should be binding and it shouldn't allow me to move um I'm a little bit confused mother with you wait a minute what is wait a minute I want to why is that I don't know I don't know it maybe it's uppercase D because I was just doing that and it wouldn't let me move on my D but okay you know what I don't understand but it's fine it's fine that's good I guess that's how it works now anyways yeah so there's other actions right also you can of the right other you can overwrite other actions like it you can see right here on the wiki it actually says you can over other actions so if you want to have H key trigger a car horn the player might also type hello and chat and use the H key of something else and then it's hard to determine if the H key so basically you'd want to bind so yeah so yeah anyways but what it's saying here to is it will make sure that the H key presses the honk action went instant most recently bound so if I were to also bind something again right to another to another key so I have to actually a whole new function but if I were to buy something to turn brick read write button press true do you do the same thing right what's gonna happen is this one's gonna overtake it this one is it's the most recently binded one because it's the bottom of the script it happened the latest rate so what's gonna happen is it's not gonna break e it's not gonna turn to brick yellow you'll turn your brick for red now so initially make sure that works and make sure I'm not wrongs like well in the ass I will turn a little function turn brick button turn brick red okay and then we'll just copy that and then we'll do and if I name them a color will do really red okay really breath there we go and now that should work I swear if it doesn't work I'm gonna be triggered not gonna lie but um and then if I were to bind that one again or actually in second but if I were to okay so it should go red dude what the heck is happening you're like it's defying its own words yeah I'm so confused oh that's why I'm so dumb I put in the on button press function okay that's my bad I really shouldn't name something better but I just need one button press but I'd need meant like button turn or actually you can do for fun I don't know I just named it you can name it button turn brick yellow that might be a little bit better syntax but anyways um I put the wrong function that's why I didn't work let me go ahead and play it again and it should work let's see and here we go and press D and then C as you can see it's red and the reason why they turn red and so the yellow even though I just bind this action as well is because like I said it's the latest behind binded function so that's what's happens also one more quick thing if I were to unbind this function so if I were to here actually I'll do it right now but if I were to go ahead and wait for 5 seconds and then and then I would unbind that function are on buying the action sorry what would happen is this action would actually take place again so this action still running in the background right well then it's not running but this actually would overtake again and now if I practice I were to press D again it would actually turn a brick yellow so you can watch right here okay so what what I did was I binded the two actions but I binded the brick on the last so I press D and then it'll turn yellow right now press D again nothing's happened or turn red sorry and now I turned yellow again because I press D again right D shoot I'm saying and that's because I unbind a dis action so whenever you if you have two actions binded of the same key the latest one's gonna want be the one that works but whenever this one binded um this will now work as well so you don't actually have to reactivate it so yeah I hope that makes sense alright but yeah also you can also you can also change the chain step is like the here let me delete all this you can change the like sets image you can change the image for the e button right so you can change the image for this one you just put in the I need to contact active service : set image and then you put the name of the action and then you put in an image which I can go ahead and go in my tool box right here and I found it earlier was there a little Pikachu for yellow right made sense there it is and it it's now on the base plate but let me go ahead and drag it into rep storage and then I'm gonna no I'll drag it into replicative storage and then let me go ahead what is HAP what okay you guys see this too right like it knows what I'm getting hacked I'm gonna hacked okay anyways now let me this and then you paste that in there and that's like a texture ID also put that in quotes and now if I go back on mobile what's gonna happen is the button will be there and it's gonna look like that right so looking test it out and make sure that works and as you can see our button is indeed an image and now I clicked it and it turned yellow right makes sense okay that's pretty much it I don't want to make anything else more confusing that's cause it's context action service very good for you when using mobile also like for example if you wanted to reload a gun that's a little bit too great to use content geyser service because then whenever the tool is equipped then you bind the action and then if it's unequipped and you unbind the action hope that makes sense right that way you're not always looking even if the guy's not equipped so yeah kind of as guys is pretty much add hope you know in the video if you did leave a like subscribe for new from awesome reluctant Orioles and yeah that's gonna be it I hope you guys enjoy I'll see in the next one peace
Info
Channel: TheDevKing
Views: 33,023
Rating: undefined out of 5
Keywords: TheDevKing, Roblox Scripting Tutorial, roblox, studio, coding, tutorial, roblox scripting tutorial beginners, thedevking roblox, how to script on roblox, roblox studio, Context Action Service, Context Action Service Roblox, roblox scripting, roblox scripting 2020, how to script on roblox 2019, how to script on roblox 2020, contextactionservice roblox, roblox contextactionservice, how to use contextactionservice roblox, how to make mobile buttons roblox
Id: y8qUoM1dyv8
Channel Id: undefined
Length: 19min 43sec (1183 seconds)
Published: Wed Jan 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.