Roblox ClickDetector Tutorial – How To Script On Roblox For Beginners – Episode 13

Roblox ClickDetector Tutorial – How To Script On Roblox For Beginners – Episode 13


In this roblox beginners scripting tutorial I’m going to be showing you how to use clickdetectors to make a button clickable so make sure that you watch this video all of the way till the end so that you don’t miss any of the important information i’m gonna give to you. we’re starting RIGHT NOW Subscribe now! so welcome to another video my name is alvinblox and today we’ll be looking at click detectors more specficailly we’ll be looking at how to make a clickdetector button which when clicked will change the colour of a baseplate to random before we begin this awesome tutorial don’t forget to subscribe adn turn on notifications bell for my youtube channel so that you never miss the best roblox scripting tutorials from me so let’s get started by heading into roblox studio and importing a new part this is going to be the base for our clickdetector you don’t need to add this but i’m gonna add it to make it more fancy all you need is just a button and then we can take it from there but if you want to make yours a bit nicer you can just add a base and then you can put your button inside it so my base is going to be quite tall just like this and im going to make it a bit thicker as well so we’re then going to insert another part this will be our button so resize it so that it just looks like a little button that’s sticking out and then you can change the colour of the button too i’m going to make mine green so that it’s nice and it stands out so what we’re now going to do is right click on the button hover on insert object and hit script now in this script we’re gonna write an event which hooks up the trigger for when the button has been clicked so to do this we will need to have a good understanding on events now if you haven’t heard of events before in roblox scripting i advise you check out my video which i did a few videos back in this series on events to find the video click the card above in the top right so if you have a great understanding or a moderate one, that’s fine, we’re only gong to look at one event today which is the mouseclick event and that is going to allow us to have some code which will execute when the button has been pressed in this case changing the baseplate colour so we hae got our script, we’re also going to need to have a clickdetector so we’re going to right click on the button, hover over insert objects and hit clickdetectors now we have everything in our game ready to code we can just head back into our script and define a variable this variable will be our click detector because we are going to want to be referencing this utie a lot in our event beacuse the cllick detector is the thing that checks for once the button has been clicked it allows the button to be clicked when you have a button and it has that cursor which looks like a hand, that is what the job of the clickdetector is it allows you to make your button clickable and it’s what the event runs on so we’re going to want to define that as a variable so we’re gonna say local clickdetector=script.Parent:WaitForChild(“ClickDetector”) now i’m wrapping this inside a waitforchild because i want to make sure that everything is loaded in my game before we begin because we don’t want anything to not be loaded due to lag while the game is starting up which could cause an error so we’re just waiting for all of our components of our part to be ready to go so now that we ahve got our variable declared we can make our event so what we’re going to say is code on screen we’re referencing the clickdetector drop a line and roblox studio should add this end) for you if yohu haven’t got it write it in yourself what we need to do now is change the baseplate colour to do this we just say code on screen what this is doing is grabbing the current brickcolour and it’s setting it to a random chosen one so that it updates every time we click the button now i’m just goingg to show you something else we can do with a clickdetector it can pass a parameter of the player who clicked the button if you don’t know what a parameter is i’m gonna summarise it click the card to learn more about parameters so a parameter is basically a piece of information which we don’t yet know the script will get it once the event has been triggered in this case we don’t know the player who clicked the button but we can use a substitute variable to fill it in so we can run some code once the event is triggered and we can substitute in the name of the player who clicked the button im inserting a parameter which is teh player who clicked it you can call it anything i’m gonna drop a line and print to the output *code on screen* i can then do a double dot and then i can do some string interpolation so i can say the player’s name clicked the butto so now if i run the code it will not only change the baseplate colour it will also print the player who clicked it this is cool because you can find the player name and show a gui to them or give them a certain tool etc in your game so that is the end of this tutorial quite a short one today, there’s longer ones in this series, just wanted to keep this one short to give you a little break 🙂 the long ones are worth the wait though so we’re gonna run the game now make sure everything is anchored and then you can click on play and click the button click it the baseplate colour changes and if we open the output it has printed that player1 has clicked the button there you go, the baseplate colour changes to a randokm colour and it prints the name of the person who clicked thanks for watching this tutorial if you want to see more videso you can click the thumbnail above on the right if you want to subscribe click the alvinblox logo on the left please also consider donating on patreon for some perks

Leave a Reply

Your email address will not be published. Required fields are marked *