Beginner's Roblox Scripting Tutorial #6 - Scope & Returning (Beginner to Pro 2019)

Поделиться
HTML-код
  • Опубликовано: 7 янв 2025

Комментарии • 1,6 тыс.

  • @TheDevKing
    @TheDevKing  5 лет назад +243

    Hey guys! Be sure to join the discord server if you have any questions or if you would like to meet me! discord.gg/FKcSyRh

    • @Brqeni
      @Brqeni 5 лет назад +2

      pls help i still dont understand

    • @drcatsiygdsfv2992
      @drcatsiygdsfv2992 4 года назад

      GetNightcored idk yet to lol

    • @Qwerty-ex2id
      @Qwerty-ex2id 4 года назад

      @GetNightcored exactly lol i m waitnig for that explanation

    • @Qwerty-ex2id
      @Qwerty-ex2id 4 года назад

      @GetNightcored nvm he explains but still idk why u should do it outside of function

    • @kanizmshossain7822
      @kanizmshossain7822 4 года назад

      @GetNightcored We use local as it is more efficient then a=2 because a=2 is a global variable and sometimes lua gets confused and doesnt use count the variable as a variable due to it being global so using local is much better

  • @robloxtutorials9187
    @robloxtutorials9187 4 года назад +803

    A good definition on returning would be a gamepass. If someone buys your gamepass, you want to make sure that they are getting something in return for buying that gamepass. So for example. You make a gamepass that gives a player maybe extra speed, then once they purchase that gamepass, they will receive it. This is also useful in GUI shops and stuff as well.

    • @robloxtutorials9187
      @robloxtutorials9187 4 года назад +52

      @@xeistan7793 You okay? I wrote this comment 2 weeks ago and had the wrong idea of returning. Your comment isn't really an insult, more just trying to look for an argument.

    • @tango3651
      @tango3651 4 года назад +56

      @@robloxtutorials9187 So that definition you said is not correct? I am confused.

    • @chukayespp6233
      @chukayespp6233 4 года назад +2

      Lol

    • @user-jd1md8no4v
      @user-jd1md8no4v 4 года назад +4

      @@xeistan7793 chill...

    • @sev9139
      @sev9139 4 года назад +8

      @Xeisn Why do you think he or she is watching these tutorials?

  • @smplxty
    @smplxty 3 года назад +373

    For anyone confused about scope here is an analogy:
    There are two students named Ave(test 1) and Eve(test 2)
    Ave has 4 pencils
    Eve has 2 pencils
    Ave has a pencil named "A"
    While Eve dosen't have any pencils named A
    When the teacher said "raise your pencils named A" (Calling the code)
    Only Ave can raise her hand (successful output)
    While Eve dosen't raise her hand (Nil)
    10 months later I finally understand returning so let me make an analogy for you:
    Code:
    Local Function HowManyMusicians ()
    Return("40 - 100")
    end
    Print(HowManyMusicians())
    Analogy:
    Let's say you're surfing Google and you want to search how many musicians there are in the orchestra, just opening Google ( calling the function ) isn't gonna automatically tell you the answer to your question, BUT if you search the question (print the function) it will tell you how many musicians there are in the orchestra
    Edit: Dont forget to add the '()' since, we would just be saying the name if we didnt
    extra:
    you can set a variable to a return:
    [RETURN]
    local function plus(x, y)
    return x + y
    end
    local n = plus(2, 3)
    print(n)
    >> 5
    -----------
    [PRINT]
    local function plus(x, y)
    print(x + y)
    end
    local n = plus(2, 3)
    print(n)
    >> nil

    • @lolh9295
      @lolh9295 2 года назад +13

      even though you commented this 10 months ago, thank you

    • @smplxty
      @smplxty 2 года назад +6

      @@lolh9295 lol saw this notif, learned returning a couple months ago but forgot to place a analogy so here it is + you're welcome :D

    • @lolh9295
      @lolh9295 2 года назад +2

      @@smplxty this is the best explanation i've heard yet, thanks a lot! i was confused about returning but i understand that wayy better now and how it could be used. Thanks!

    • @smplxty
      @smplxty 2 года назад +1

      @@lolh9295 You're welcome!

    • @3man3
      @3man3 2 года назад

      @@smplxty i folllwed that returning script and it prints function: 0x8f004ccc88a0cdb0 - server - script:5

  • @aydonread5915
    @aydonread5915 3 года назад +197

    The keyword return can be used to take information from a function, and allow it to be used wherever the function was called originally. If your friend asked you for something in another room, you would go get it and bring it back. When a function is called, the script goes to the function, and then returns with a value.

    • @reeceh_27
      @reeceh_27 2 года назад +6

      wow, tysm great summary!

    • @aydonread5915
      @aydonread5915 2 года назад +3

      @@reeceh_27 no man

    • @aydonread5915
      @aydonread5915 2 года назад +5

      @@reeceh_27 no problem**

    • @Clownzyy
      @Clownzyy 2 года назад +2

      THANK YOU!

    • @noobwave6027
      @noobwave6027 2 года назад +3

      Tysm for the example now i understand!

  • @GondoMan21
    @GondoMan21 4 года назад +430

    This is what I understand from Returning;
    Returning: in a function thats a line of code that doesn't activate unless its equal to a variable
    example;
    local function bruh2()
    print("hi")
    return "lol hi"
    end
    bruh2() - wont show return but will show others like print
    local Hotdog = bruh2() - makes the call into a variable
    print(Hotdog) - then will show whatever in return

    • @retrofilmwork
      @retrofilmwork 4 года назад +45

      so a function must be inside a variable to activate all returns?

    • @GondoMan21
      @GondoMan21 4 года назад +25

      RETRO STUDIOS yeah

    • @Qwerty-ex2id
      @Qwerty-ex2id 4 года назад +23

      ok that helps it was what i was thinking it is and when i ask a server with good scripters none of them can explain it like normal humans without making it more confusing this is all i needed and i got it now

    • @swiftyhackz951
      @swiftyhackz951 4 года назад +15

      bruh that was good BRUH!! xD

    • @mattmarafino
      @mattmarafino 4 года назад +10

      Its helpful for remote functions when you need either a true or false value. It's pretty basic in concept but advanced in use

  • @nicksennn
    @nicksennn 4 года назад +118

    My own example of when to use a return is like
    local function question() -->asking a question
    print("Whats ur name?")
    return("Bob") -->what the respond is written
    end
    print(question) -->gonna print the question but not the respond/return
    local respond = question()
    print(respond)--> is only gonna print the 'return' command
    and so, the output is going to be:
    What's ur name (From the 'print(question)'
    Bob (From the 'print(respond)"

    • @littleretroship6403
      @littleretroship6403 4 года назад +10

      Thank you so much for the explanation! It really helped me with the return

    • @articticcblu
      @articticcblu 3 года назад +1

      Ah makes sense

    • @articticcblu
      @articticcblu 3 года назад +1

      The script exuctes by lines so the function goes first before the local so it prints gain

    • @jordankidd230
      @jordankidd230 3 года назад

      @gangss just put question() at the bottom to call the function and then you will get ("whats ur name")

    • @skeptical_playz6142
      @skeptical_playz6142 3 года назад

      @@jordankidd230 I tried it, he’s right

  • @dragonmaster4548
    @dragonmaster4548 5 лет назад +94

    Now I get the whole vid from watching it again, ty so much. XD

    • @cosmodream580
      @cosmodream580 5 лет назад +1

      Ya me too but I still don't quite understand return. Like do you need to have a variable to call the function with the return or what?

    • @robloxllany4923
      @robloxllany4923 4 года назад

      I love ur profile picture

    • @curuptedv.256
      @curuptedv.256 4 года назад

      7 is cool

    • @curuptedv.256
      @curuptedv.256 4 года назад

      Lol script

    • @curuptedv.256
      @curuptedv.256 4 года назад

      @Union Centauri mlm me nerd

  • @somenicemilk8965
    @somenicemilk8965 5 лет назад +50

    Your tutorials are amazing! Others are too fast-paced and have holes but yours are very helpful. I was confused at returning at first but after some practice I understood it. Thank you :)

  • @alongal407
    @alongal407 4 года назад +151

    I came back to this video after a long time to help viewers understand returning: a good example of returning is when you need to use two values, but each of them is in a different function, so for example I wanna make a script that calculates mass, the function will run, and when it ends it would need to do something with the information it calculated, it cant just print it, because then the player wont see it, so it returns the mass so then you can use it later in the script and put it in a gui or something

    • @kingpig8732
      @kingpig8732 3 года назад

      wait so if you wanted to use it later in the script do you put return 'function' after the part you want to return or the place you are putting the value?

    • @moldybread2445
      @moldybread2445 3 года назад

      hi i was a bit confused of what scope does. could u define it a bit.. thank you!

    • @sarimahsan6962
      @sarimahsan6962 3 года назад +5

      @@moldybread2445 sorry if you still dont understand
      when we use local it means it can be accessed anywhere in the script but if we put that variable inside a function and remove the local it is only accessible to that function meaning if you try to call it outside the function or in another function it doesn’t work and instead it says nil and that means no value.So in the end what i'm trying to say is local means it’s accessible anywhere but if it doesn’t have a local it has to be in a function or dependent on something to work and it will only for the thing it’s dependent on nothing else.

    • @_RJ_
      @_RJ_ 3 года назад +16

      i think this is what alon means (im also new sry if there are errors in code)
      local density = 0
      local mass = 36
      local volume = 42
      local function density()
      density = mass / volume
      return density
      end
      local CowDensity = density()
      if CowDensity < 1 then
      print("Float")
      else
      print ("Sink")
      end

    • @coswic9128
      @coswic9128 3 года назад +2

      OooOOoOoOoH That makes sense

  • @on_edge5678
    @on_edge5678 4 года назад +3

    first playlist of tutorials that actually make sense, other ones are like "type local and this and that" expecting you already have knowledge on scripting. Thank you for taking the time to explain everything there is to know about scripting

  • @billiam6398
    @billiam6398 2 года назад +8

    i’m years late, but the straight definition is what helped me figure out returning.
    if you’re using your function many times throughout your script and need to do different things with the outputs, then returning is essential.
    what returning does is just in the name; it returns the output to the call, rather than doing anything with it by itself, which is very useful when you want to run different things through the function for different purposes (i.e. a rather convoluted rounding script)

    • @seiari693
      @seiari693 2 года назад

      ty

    • @Cosmicious
      @Cosmicious 2 года назад

      I think I got you but could you give an example?

    • @billiam6398
      @billiam6398 2 года назад

      @@Cosmicious let’s say you’re making a rounding function, and use it throughout your script for various purposes.
      your function would return the rounded value rather than do anything like printing it. later, if you wanted to print the rounded form of a value, you would call the function in place of the regular input for printing; it would look something like this.
      print(functionname(24.2))
      in this case, you’re calling a function named functionname with an input of 24.2, and the function returns a value to where it’s called, in this case into a print command. the print command recieves the value from the function, and prints said value.

    • @eeeEEEeeeEEEEEE-eeeeeee
      @eeeEEEeeeEEEEEE-eeeeeee 8 месяцев назад

      this is not straight i am confused

    • @billiam6398
      @billiam6398 8 месяцев назад

      @@eeeEEEeeeEEEEEE-eeeeeee you can call a function as an argument in another statement, and the value the function returns will be used.

  • @unclesam1545
    @unclesam1545 3 года назад +27

    So for those who don't understand return it is basically the "result" of a function, the value it gives you after the machine elaborates the script inside.
    To understand it better, for example math.rad is a function, and it "returns" the rad of the number in input; it is basically an output.
    hope i helped u

    • @cyro5535
      @cyro5535 3 года назад +2

      so like replace hotdog with conversation and so it prints hi and the conversation equals hi because its part of a conversation you started so in return the other person is replying to you saying awesome. Is this a good definition of it?

    • @Zakorree
      @Zakorree Год назад

      @mikela5025 ye i think so

  • @duckq9989
    @duckq9989 4 года назад +283

    I’m gonna watch this playlist 3 times so I fully understand

  • @pocleaf
    @pocleaf Год назад +97

    If you are still confused about returning,
    So example this, a player touched a block and another block will turn off can collide and the transparency to 0, when you used return , after the player is done touching the block the return will terminate the function and returns to normal. When i mean by normal it resets to the very beginning, it resets to cancollide on and the transparency to 1, like reverting everything back like it used to be!

  • @ye945
    @ye945 4 года назад +142

    4:20
    Don't mind this, it's just a timestamp for me to remember when the returning starts
    (Also 420 lol)

    • @Eri.262
      @Eri.262 4 года назад +1

      Thankyou I think we all needed to see that twice lol

    • @user-jd1md8no4v
      @user-jd1md8no4v 4 года назад

      tyty

    • @cosmicblox934
      @cosmicblox934 4 года назад +1

      we cant understand it cuz the return code is outdated

    • @pepsiman4071
      @pepsiman4071 4 года назад +1

      420 lol

    • @cosmicblox934
      @cosmicblox934 4 года назад +1

      @@HarryDKH well I fixed it I am on the advanced.seriris thanks for trying to help me

  • @dirhi
    @dirhi Год назад +42

    For those who still dont get returning:
    Lets say there is a function, "return()"
    This function returns 1
    If you just call the function, nothing will happen.
    Its like if you just put "1" in your script.
    Nothing will happen.
    However, you can assign it to a variable, or print it.
    Therefore, "print(return())" is the same as "print(1)" (in this case)
    Basically, returning in a function makes the function a placeholder for what it returns.
    Also, I havent watched the whole video so I am not sure if it works like this in lua, but if you use the "return" statement in the middle of a function, the lines of code after it (in the function) will not run when you call the function. At least that is how it works in the other 4 programming languages I am familiar with.
    You may be wondering, "Why not just print the value?"
    This is because you may not want the value to be printed. Maybe you want it to be the amount of damage you take or something like that.
    You can use a variable for that. However, you dont want to write out the equation every time you use that value. True, you can just reuse the variable, but you might have to change the value.
    If you use a function, you can make it return the calculated value, and store that in a variable.
    This makes it much easier to work with the script.
    I hope this helped :)
    (I suck at teaching others so if this did not help then I understand why lol)

    • @MrBaadshah2000
      @MrBaadshah2000 Год назад +8

      This is the only comment I understood, thanks!

    • @HugeHippo
      @HugeHippo Год назад +4

      I still struggled understanding until you explained about the damage, thanks!

    • @dirhi
      @dirhi Год назад +2

      :)

    • @Seraphius
      @Seraphius Год назад +2

      O thanks, u made me understand returning better!

    • @frownyee
      @frownyee Год назад +1

      I’ll be honest I still don’t understand
      can you give a shorter example?

  • @ProducingItOfficial
    @ProducingItOfficial 4 года назад +43

    So what I understand from what returning is, I guess its basically when you want to use a return script in a function, you must assign a variable to the function that uses the return script. And then you must print out that variable to finally see the return script running. Kinda complicated in my opinion but that's probably because I'm just barely learning about this.
    Edit: I have a much better understanding of returning now.

    • @cyro5535
      @cyro5535 3 года назад +9

      so like replace hotdog with conversation and so it prints hi and the conversation equals hi because its part of a conversation you started so in return the other person is replying to you saying awesome. Is this a good definition about what its about?

    • @gabrielc7861
      @gabrielc7861 2 года назад

      So If I get this right returning is used to make something equal something else but only if conditions are met first unlike variables?

    • @detcom9140
      @detcom9140 Год назад

      @@cyro5535 thanks I get it now

  • @pasteteOhr
    @pasteteOhr 2 месяца назад

    ill be honest, you explain this much better than anyone else. ive watched 2 scripting playlists and i started this one and this is by far the best and also gives most information in little time

  • @7emy255
    @7emy255 3 года назад +5

    I watched this like 8 times and I finally understand it!!

  • @yomano9146
    @yomano9146 3 года назад +2

    You make it so simple thats why you are my favorite roblox Scripting RUclipsr

  • @Redeight
    @Redeight 2 года назад +18

    Return is used to store data that can be used later on if u want to do changes on the function u wrote. Remeber to make a variable for the function to make return work.

    • @Flamecasts
      @Flamecasts 2 года назад +3

      i understand now tysm daddy

    • @godrole2481
      @godrole2481 2 года назад +2

      @@Flamecasts ayo

    • @9Sparked
      @9Sparked Месяц назад

      @@godrole2481 oya

  • @Illum_ina
    @Illum_ina 2 года назад +1

    This dude learned me more than those 1 hour long vids. Thank you so much!

  • @hobovan.
    @hobovan. 4 года назад +86

    4:20 gotta *return* to this a few times... *laugh track plays*
    Edit: only just now came back to this lmao, and this joke is just 👁👄👁

  • @valentinpetrov9759
    @valentinpetrov9759 5 месяцев назад

    you saying you struggled with returns and me getting a good grasp on it, either you're an amazing teacher or this is my calling xD

  • @chopper-go8pf
    @chopper-go8pf 8 месяцев назад +2

    Oh my god it is freaking tricky, I literally sat down for 30 minutes alone just to figure it out hahahaa thanks bro

  • @Chromish1
    @Chromish1 8 месяцев назад +1

    W video. You are an amazing teacher, you prob won't see this, I am a few years late to watching this video. But I can confidently say, that I understand everything you have discussed. Thank you :)

  • @skyface5899
    @skyface5899 4 года назад +3

    *What I understand from returning:*
    So there is a function called "hi". And you set a variable that is equal to "hi", which is the said function. So by printing out the variable that is equal to the function, you'll activate it (Since you printed out something that's equal to the function, could be considered as calling it and you know, it activates the function as we see).

  • @patroneran9997
    @patroneran9997 22 дня назад

    HUGE tip for that actually helped me understand everything: at the end of each video, experiment with what you just learned. literally do anything it doesnt matter you just have to make stuff with what you learned to really get it in your head and actually learn it because knowledge is not everything, parctice is everything.

  • @BwithTea
    @BwithTea 3 года назад +10

    I know I am super late, but I am taking a course on scripting. The way I learned it was making the function like a math function. So
    --addnums is telling the system to add these two variables
    local function addnums(n1,n2)
    --You are telling the system to return this function
    return (n1,+n2)
    end
    --You are setting the answer to this function with the numbers in place of the variables
    local answer = addnums(1,2)
    --Finally, you print the answer
    print(answer)
    This should result to the answer being printed to 3. Another version of this same function is this
    --addnums is telling the system to add these two variables
    local function addnums(n1,n2)
    --You are telling the system to return this function, this time you break it down
    return n1..("+")..n2..("=")..(n1+n2) ------------------------------------------------------------------------------------------------|
    end |
    --You are setting the answer to this function with the numbers in place of the variables |
    local answer = addnums(1,2) |
    --Finally, you print the answer |
    print(answer) |
    |
    This result will yield this answer, 1+2=3. It will return whatever you have on that return line.------------------|
    My example should give you a good understanding because it is like a math function. You can make sense of this because it is simple arithmetic. When my teacher showed me this, I had and "aha" moment.
    If you want to try more math examples then just replace "addnums" with "subtractnums", "dividenums", "multiplynums". Make sure the symbols match up too, for the function.
    Hope this helped. :D

    • @lucky_zak2426
      @lucky_zak2426 3 года назад +2

      Thanks! it helped!

    • @BwithTea
      @BwithTea 3 года назад +1

      @@lucky_zak2426 Glad I could help!

    • @jojo_gaming234
      @jojo_gaming234 6 месяцев назад

      ​@@BwithTea hi can I tell you what I did understand from return.. I want to make sure I understand it right

  • @mladenk6069
    @mladenk6069 2 года назад +12

    This is what I think return is:
    -Return won't be seen by players, unless we add it to a variable.
    1 - local function hi()
    2 - print(hi)
    3 - return "Awesome"
    4 - end
    5 - hi() ---> it's only gonna print hi and return Awesome but we can't see it so we will make a variable to print Awesome
    5 - local HotDog = hi()
    6 - print(HotDog) -----> now it's gonna print Awesome because it knows what Awesome is due to the hi() function
    This is only my thinking and I can be wrong but if someone who knows what return is can they comment and tell me if I'm right or wrong?
    Thank you

    • @kasatero
      @kasatero 2 года назад +1

      thank you

    • @mladenk6069
      @mladenk6069 2 года назад +1

      @@kasatero no problem

    • @detcom9140
      @detcom9140 Год назад

      yeah that’s what I got from this video too

  • @almamushrooms
    @almamushrooms 9 месяцев назад +2

    i have been struggling to understand returning for a long time but when i watched your video i finally understood the concept. thank you so much!

  • @martiny6620
    @martiny6620 9 месяцев назад

    this is the best video that explains returning imo.. every other tutorial was rather difficult

  • @NotOG_
    @NotOG_ 3 года назад +3

    #Question if I don't use the return keyword will the function not be able to go inside a variable?

    • @WaterZzz
      @WaterZzz 11 месяцев назад +2

      Yo Idk you were coding back in the day💀

    • @Liane463
      @Liane463 7 месяцев назад

      LOL

    • @Opterer_Official
      @Opterer_Official 7 месяцев назад

      Blud gave up tryna learn to code and went to play blox fruits instead

    • @OnePiece_Dimension
      @OnePiece_Dimension 6 месяцев назад +1

      ggs on you
      ´´

  • @diamondgaming470
    @diamondgaming470 Год назад

    I know I’m like 5 YEARS late but these are the BEST videos for learning lua and Roblox studio stuff I understand almost everything you say and your so simple and straight to the point this is very helpful 🌈

  • @itzploopy3213
    @itzploopy3213 Год назад +5

    I see so many comments not explaining this right. So let me tell you so basically lets say you make a function that will spawn a part and after that function is finished you probably want to access the part later on to maybe change it's color or size. You can't just get the part from the function because its only defined inside that function, so what you can do is return your part after the function is finshed so you can access that part later on. so your varaible that you made for example devking made a varaible = to his function that varaible will now basically define as your part. keep in mind the function still runs even though its in a varaible

  • @mranonymous8940
    @mranonymous8940 2 года назад

    Omg this is the best playlist i have ever looked at!Your just the right guy for these kind of things.Thanks so much!!!

  • @brocockman5116
    @brocockman5116 2 года назад +3

    Also for anyone wondering the differnce between local function and regular function is that local function can only be accessed in that script (accessed as in like calling the function)

    • @Is-de6qk
      @Is-de6qk 2 года назад

      What's the difference between a variable and a function?

    • @Is-de6qk
      @Is-de6qk 2 года назад

      Btw so if i do a function(not a local function) i can call that function in another script such as a serverside script?

    • @detcom9140
      @detcom9140 Год назад

      Ty

  • @PaliLyrics
    @PaliLyrics Год назад

    bro you are awesome. I am interested in coding languages for a year i guess and i never understood return. I understood it after i watched your video. Thank you so much!

  • @katkant
    @katkant 3 года назад +22

    Notes:
    The location of *”local”* matter
    Explanation: if local is not inside, let’s say, a function, then the whole scripts knows it.
    But if it’s inside a function, then the script doesn’t know it, but only the function does.
    But if you don’t use “local” it will just let the whole script know. (But will function slower)
    Creds to @Alon Gal for the following:
    A function will run, and when it
    ends it would need to do something with
    the information it calculated, so it returns the mass so then you can
    use it later in the script and put it in a gui
    or something
    Example:
    Function named Example has code in it
    At the end or somewhere it says “return *anything*”
    The function is now worth *anything*

    • @starpeep5769
      @starpeep5769 2 года назад

      Let's define local
      local literally is - belonging or relating to a particular area or neighborhood, typically exclusively so.
      an inhabitant of a particular area or neighborhood. so like we use it as in our vicinity, most nearest
      so lets use that to define local in scripting - whatever its in, like the script is the neighborhood, its only in that neighborhood so it only functions there, like when its in a function

  • @JonasBellaert
    @JonasBellaert 4 года назад +1

    For people that doesn't understand returning read this,
    If you place a function inside a variable the variable will be equal to the line of code you returned.
    here an example (More explaining at the bottom)
    local function test()
    return game.Workspace.Baseplate
    end
    local variable = test()
    variable.Transparency = 0.5
    --Function name is equal to test. In the function we typed a line of code in this example game.Workspace.Baseplate
    we placed return before it.
    --outside of the variable we created a variable that's equal to the name of the function. Now the script will look inside our functions and will scan for the line of code we returned. now the variable wil ---be equal to that line we returned. so we now know the variable is equal to the returned line of code and that's variable = game.Workspace.Baseplate
    --This is the location of the Baseplate so if we now do variable.Transparency = 0.5 he will change the Transparency to 0.5 because he knows variable is equals to the returned line of code in the test ----function.
    --(Still don't understand ask your questions down here)

    • @nateguop
      @nateguop 4 года назад +1

      you made this so much easier to understand thx

    • @JonasBellaert
      @JonasBellaert 4 года назад

      @@nateguop thx 😊

  • @honoiki3778
    @honoiki3778 3 года назад +3

    Here's an example of what return could be useful for:
    function something(a, b)
    return (a+b), (a-b), (a*b), (a/b)

    end
    c, d, e, f = something(6, 3)
    print(c)
    print(d)
    print(e)
    print(f)

  • @Vato_Hey
    @Vato_Hey Год назад

    It has been months i stoped watching your tutorials but i'm back and later or sooner i heard that roblox studio uses programing language called "Lua" and i'm learning that language and your videos help me very much!

  • @crusherofrocks3906
    @crusherofrocks3906 4 года назад +48

    what I heard devking say: it has o start with scope. me: puts scope on my sniper ah yes I knew this would come in handy.

  • @joshuaprogamer1170
    @joshuaprogamer1170 5 месяцев назад

    you explained returning like a boss appreciate you, im starting my journey as a solo dev

  • @zakiiy
    @zakiiy 2 года назад +3

    Tutorial 1-3:
    Me: Haha that's easy.
    Tutorial 6+: SIKE
    Me: MY BRAIN

    • @ehtz
      @ehtz 2 года назад +2

      i feel so dumb because everyone can understand this and this is my 3rd time watching and it’s still hard for me

    • @zakiiy
      @zakiiy 2 года назад +3

      @@ehtz You'll understand it someday, keep up the work

    • @ehtz
      @ehtz 2 года назад +1

      it is the 9th time (i counted) watching this and i still do not know what scoping is and why this tutorial came 15 tutorials before learning how to make a tool

    • @zakiiy
      @zakiiy 2 года назад +1

      @@ehtz I can tell you are hard working, you might wanna skip this one until U learn the basics then come back to this

  • @deleted_account-u3w
    @deleted_account-u3w 2 года назад

    I’m watching this as an experienced developer (who forgot lua) and I would recommend this series to anyone who wants to learn scripting in roblox

  • @beech7392
    @beech7392 4 года назад +8

    Me:*understand what returns are but couldn't explain it to someone if they asked*

  • @LILaussieJ
    @LILaussieJ 3 месяца назад

    DUDE I HAVE BEEN TRYING TO LEARN SCRIPTING FOR SOO LONG U HAVE NO IDEA TYSM

  • @PigNe0
    @PigNe0 4 года назад +4

    4:36, no it won't because it is not in quotation marks to make it a string!

    • @jassskmaster7575
      @jassskmaster7575 4 года назад

      ya it would just print something like 'function 0x33addf3000a"

    • @margaretplayz2179
      @margaretplayz2179 4 года назад

      @@jassskmaster7575 same

    • @channeloft
      @channeloft 2 года назад

      Hi is a variable so it doesn't need quotation marks

    • @Gavblox
      @Gavblox Год назад

      Man got his own script wrong

  • @Crimlask
    @Crimlask Год назад

    It's hard to understand the thing for a moment but as soon you explain more and more it gets More understanding

  • @diamondisvibing3072
    @diamondisvibing3072 3 года назад +3

    3:32 how come when dev king takes out the local it starts working for both blocks of code?

  • @NebulaStormer
    @NebulaStormer 4 месяца назад +1

    If someone still doesn't understand what return does, here is a good explanation:
    return is like a way of giving back a result from a function. Think of it as a function being a machine that does some work. When you use return, you’re telling the machine what the final answer or result should be.
    Here’s a simple example:
    function addNumbers(a, b)
    return a + b
    end
    In this example, the addNumbers function takes two numbers, adds them together, and then returns the result. So, when you call addNumbers(3, 4), it will give back 7.
    In short, return is used to send a result back from a function to where it was called.

  • @isaachodnett2357
    @isaachodnett2357 3 года назад +9

    Just a quick tip for anyone that is reading this and doesn't understand what TheDevKing is doing. PRACTICE!
    Don't spend a couple of seconds on each task spend as long as it takes for you to understand this. I had to dedicate 15 minutes changing the place you put the local to understand this. Honestly, it just get's easier after time and once you understand you feel like you have climbed Mt Everest.
    (I haven't climbed Mt Everest)

  • @idonotknowwhattoputhere6941
    @idonotknowwhattoputhere6941 3 года назад

    This dude is explaining things in minutes but other RUclipsrs are taking an hour just to explain one line of code this dude explains the whole script

  • @mortada2008mansour
    @mortada2008mansour 5 лет назад +6

    Can you give us a example? Like u r gonna build something and u have to use the returning

  • @lukethornburg3604
    @lukethornburg3604 5 лет назад +8

    Do you have a discord server or something for this so its easier to ask questions and stuff?

  • @jo-xt4xb
    @jo-xt4xb 3 года назад +2

    I'm hoping this might help someone, after rewatching it means that basically, when you print Hotdog in this case, it's looking for the value of hi() since hi() is a variable and if you return "Awesome" in this case, "Awesome" then becomes the value meaning it prints "Awesome"
    I'm very sorry if this is wrong or doesn't help but I tried lol

    • @stuntfax9004
      @stuntfax9004 2 года назад

      And that’s what we like trying

  • @jacob2604
    @jacob2604 4 года назад +5

    wait, so basically a return lets your code run through then finished the value?
    For example.
    Let say you want to make sure your code works, add a return at the end so if it runs through the code and give the return your code works.
    Am i correct?

    • @zkn8665
      @zkn8665 3 года назад

      What the fuck are you talking about

    • @jacob2604
      @jacob2604 3 года назад

      @@zkn8665 why the fuck you respond 8 months later.

    • @Batuh_n
      @Batuh_n 3 года назад

      why the fuck didn't I understand

    • @jacob2604
      @jacob2604 3 года назад

      @@Batuh_n I have no fucking clue

    • @WillowDevelops
      @WillowDevelops 3 года назад

      @@jacob2604 Are you an advanced scripter now that this post has been over a year?
      The reason I ask, is because I assume you followed the rest of these tutorials, and I want to know if they helped.

  • @Coja54321
    @Coja54321 10 месяцев назад

    Learning c language in school really helps with understanding this, i can freely say its easier since u only have local instead of int double chat etc. and u dont need " ; " at the end almost every single line of code. Also nice tutorials man!

  • @billybobmcginty9879
    @billybobmcginty9879 5 лет назад +6

    I have a really important question how to you make a script run from walking on a part

    • @kingnae5383
      @kingnae5383 4 года назад

      You'll need whatever_you_named_the_part.position = vector3(position)
      You'll have also to call the humanoid

    • @hamster8706
      @hamster8706 4 года назад

      *workspace* .YOURPARTNAMEHERE.Touched:Connect( *function* ()
      --_Your scripts here_
      end

  • @F0rs33nvids
    @F0rs33nvids Год назад +1

    If you dont understand returning basically what it does it returnes the data or value of somthing
    So
    Local function hi()
    Print ("hi")
    --blah blah blah
    Return "awesome"
    End
    Local hotdog = hi ()
    Print (hotdog)
    Basically when the game runs through the script it creats a function named hi and in the function it prints hi
    Then it goes through the return awesome command (we willl get to that in a sec), then it ends ths function and then it creates a variable named hotdog and hotdog is equal to the function named hi. Then it will print hotdogs but hotdogs is equal to the function hi so it will print the function and basically when it does that it will print "hi" because thats whats in the function and it will also print awesome because it is returning the value of it

  • @DodDev
    @DodDev 7 месяцев назад +4

    who is looking at this in 2024
    👇

  • @lilahmae411
    @lilahmae411 5 лет назад +2

    Thanks for doing this scripting series its helping me alot learn about scripting. I havent been having any troubles thanks so much TheDevKing! Your the only one who explains in correct in a video for ne

  • @abdulrehman-px6tw
    @abdulrehman-px6tw 4 года назад +5

    example of return (put in a local script)
    local name = game.Players.LocalPlayer
    local function hi ()
    print ("Hey Bill")
    return(name.Name)
    end
    hi()
    local response = "Hey..whats your name Joe? Oh ye I remember you from Jailbreak, Hey "..hi()
    print (response)

    • @sammymillan6085
      @sammymillan6085 4 года назад +1

      yeah I think you just made it more complicated

  • @blouky2115
    @blouky2115 2 месяца назад +2

    (I can be wrong) return for me is like used to gather information from the function or multiple functions
    EX:
    local function number1()
    local var = 5
    local var2 = 25
    return var + var2
    end
    local function number2()
    local var = 10
    return var
    end
    number1()
    number2()
    local numbercombination = number1() + number2()
    print(numbercombination)
    OUTPUT: 40
    Script runs the 2 functions, but return gathers the variables ive set for return, i then use a "local numbercombination" variable to gather these values outside the function, and then i can use these values to do anything like lets say one function was used to calculate your total earnings, and the other function was used to calculate your losses, i can use the values gathered to print the total losses ive made, and so on. I mostly thought while reading the comments was just for debugging code, but i think it can be used like this to. anyways im just starting to learn roblox lua, since ive used visual scripting on another game engine like GDevelop ive got a some understanding on things, but not code related. But i hope this helps someone understand white i think Return function is.

  • @xxrice_pickerxx2720
    @xxrice_pickerxx2720 4 года назад +3

    Can someone give me a good example of scope because I don’t understand what it does but I did understand the code part of it

    • @shiz420
      @shiz420 4 года назад +3

      example: (idk if this is correct)
      function blah()
      local a = 1
      print(a)
      end
      function blahVersion2()
      print(a)
      end
      the first function will print a, but the second one doesnt know what a is. but in the first function if you put a = 1 instead of local a = 1 then the second function would know what a is too, and both functions would print 1

    • @xxrice_pickerxx2720
      @xxrice_pickerxx2720 4 года назад +1

      Hayden Boshoff I understand the script I don’t understand what you would use it for do you know a more realistic use of scope like in a real roblox game

    • @bacowedthebull3853
      @bacowedthebull3853 6 месяцев назад +1

      @@xxrice_pickerxx2720you can do this with other stuff I think not just printing

    • @Xyption
      @Xyption 3 месяца назад

      I think it if you want 1 answer in a function I’m still super confused

    • @Xyption
      @Xyption 3 месяца назад

      @@xxrice_pickerxx2720can you tell me now bc I’m 11 and just starting coding and Mabye u know now I’m still stuck on everything

  • @Brrradden
    @Brrradden 3 года назад

    ngl i learned a lot from each and every video
    cant wait to see what happens next

  • @josh.terner
    @josh.terner 4 года назад +4

    So is local just used when you want to save an action in a function and Scope just used to save it throughout the whole script?

    • @bruheleven
      @bruheleven 4 года назад +3

      could say that, or simply if you want the whole script to know a string/number you can type
      local a = 3
      local function
      print(a)
      end
      this will print 2 because it is available to the entire script but if you put
      local function
      local a = 2
      print(a)
      end
      then anything inside the local function has access to a if it makes sense

    • @calebforehand6159
      @calebforehand6159 4 года назад

      Basically a shortcut like u can make on ur phone where u could type idk and it auto corrects to I don’t know

  • @Antstosia
    @Antstosia 3 года назад +1

    I think I understand returning, when you make the variable equal to the function, they can both communicate. So the function will have something to return the string to, so it will return the string "Awesome" to the variable, and then the variable can print it for the function.

  • @mimat_
    @mimat_ 5 лет назад +13

    local gametime% () 10:23
    idk about this but does this work?

    • @curuptedv.256
      @curuptedv.256 4 года назад

      No replays in 1 year omg u got would record bud

    • @TheDrghu
      @TheDrghu 4 года назад

      @@curuptedv.256 lmao what a fucking loser amirite

    • @Ynotnbe
      @Ynotnbe 3 года назад

      @@TheDrghu ikr

  • @davitorres6756
    @davitorres6756 2 года назад

    dude I did not understand it at first, I had to repeat this part of the return and even stick the explanation to understand! but in the end I succeeded, thanks!

  • @carter7058
    @carter7058 4 года назад +16

    This is so difficult to pick up lol

    • @Zenith_Nulls
      @Zenith_Nulls 3 года назад +5

      so basically a return will not activate unless the function its under equals something else

    • @ExcuseMe1
      @ExcuseMe1 3 года назад +2

      @@Zenith_Nulls thx

  • @SweetstogoOfficials
    @SweetstogoOfficials 5 месяцев назад

    omg ty so much i have been on a problem script for making a game this is so much betteer than every tutorial and its short

  • @RealProjectRBX
    @RealProjectRBX 5 месяцев назад +12

    who us here in 2024

  • @ShermanMark1
    @ShermanMark1 2 года назад

    I have been struggling with Returning but now that I have seen this I do understand them now

  • @jerwingoh324
    @jerwingoh324 4 года назад +7

    Wow, this is actually similar to python.

    • @gagebenker2946
      @gagebenker2946 2 года назад +1

      Yeah, I've been noticing that too. I have some knowledge of Python so I was able to catch onto this very quickly.

  • @creativemusic362
    @creativemusic362 2 года назад +1

    Basically, if you don't understand, returning is very helpful for storing data. It can also help in printing data out immediately. For example:
    local function firstGroup(sun, mercury, venus, earth, mars) - - creates a function and gives it parameters for later use
    return sun+mercury+venus+earth+mars - - Here we want to calculate the total weight of our given parameters and return them
    end
    returnFirstGroup = firstGroup(2000, 1, 1.6, 2.5, 2.4) - - Firstly, we give the parameters numbers each, e.g: (Weight of the Sun is 2000 units, while the weight of Mercury is 1 unit, etc...) Secondly, we calculate the total mass and store them into a variable for later use
    local function change(a) - - Let's say we calculated the weight wrongly and we are supposed to divide the total mass by 2. We can create a function to change the weight. We also give another parameter for our new function that will be used for diving the total mass.
    return returnFirstGroup / a
    end
    finalMass = change(2) - - We then divide the mass by 2 and store it as a variable.
    print(finalMass) - - Finally, we print the new mass.
    Fun fact: You can also shorten the last 2 lines of code by printing our function, for example:
    finalMass = change(2)
    print(finalMass)
    =>
    print(change(2))
    Hope it helps! 😊 (even though I'm 2 years late lol)

  • @viper5409
    @viper5409 5 лет назад +11

    So, when you return "Awesome", you basicly replace the script with "Awesome" ?

    • @TheLostWanderer705
      @TheLostWanderer705 5 лет назад +6

      From what i understand, Functions can be used normally if they called by their normal name, when returning, it does whats written in return

  • @lynkkx
    @lynkkx 3 года назад +1

    does he still answer questions in these? so local restricts the variable/function for use within the script or code its placed in right, so does the a=2 variable without local, can other scripts access that since its not local within the script..
    2nd question is on the example with hi and awesome, 6:15, why was hi still printed even tho the hi function wasn't called, all he did was set the function as a variable and print the variable so the print("hi") within the function should not have executed without being called right ?

  • @donttimemeou
    @donttimemeou 4 года назад +3

    But if u use this while instancing can u do
    Return chickennuggets
    And it would return the part as the name chicken nuggets?

  • @trulyka5534
    @trulyka5534 7 месяцев назад

    In the way I understood it clearly: the return statement does indicate that the function has completed its task, its primary purpose is to provide a mechanism for passing results back to the caller and controlling the flow of the function execution.

  • @graysonturner186
    @graysonturner186 4 года назад +9

    I feel like the more videos I get into the more quiet his voice is..

  • @CornBallEditz55.GoofyAhh
    @CornBallEditz55.GoofyAhh 3 месяца назад

    for those who did't understand. when you use the varible that was = to the function. if you then do something like this:
    local function hi( )
    print("hi")
    return "awsome"
    end
    local hi_function = hi( )
    print(hi_function)
    hi_function would be = to the returned value. which in this case was "awsome". so when i printed hi_function it printed awsome cuse thats the return

  • @l3g3nd59
    @l3g3nd59 6 лет назад +9

    wait why cant i just use this code
    local function hi()
    print("hi")
    print("awesome")
    end
    hi()
    doesn't it do the same thing and shorter code??

    • @TheDevKing
      @TheDevKing  6 лет назад +6

      Yes it does! and for printing it will work just fine. But I was just using printing as an example. I guess printing was kind of a bad example but Returning is useful when you need values from functions. Just think of it like that. It doesn't always have to be printed.

    • @l3g3nd59
      @l3g3nd59 6 лет назад +1

      ooh ty!

    • @zhkingg
      @zhkingg 5 лет назад +1

      returning is useful for lets say.. math.
      local function Double(numbertodouble)
      return numbertodouble *2
      end
      --i want to double 2, lets run it through the function!
      local numberiwanttodouble = 2
      local answer = Double(numberiwanttodouble)
      print(answer)

    • @flashstryker1
      @flashstryker1 5 лет назад

      I tested it, it works and print's all variables. Since hi was identified the local function was to run both prints meaning both were printed.

  • @d1eoim
    @d1eoim 4 месяца назад

    thank you so much!!!! maybe 5 years later but im alwawys so happy when i understand something

  • @Airbus23
    @Airbus23 3 года назад +99

    this is how many people watching are kids

  • @unknownedleaf
    @unknownedleaf 11 месяцев назад +1

    correct me if im wrong but i think return is to make yourself aware that the output of the function is from a variable.
    if you run the function regularly, the Awesome wont show so you know its being run regularly.
    if you run the function through the variable, the Awesome will show so you know its being run through a variable.
    say you got an apple from your front door but you want to know who its from. there are two apple givers and you told the company that gives apples to attach an extra letter to the apple if it is from apple giver B.

  • @Predsfan
    @Predsfan 3 года назад +4

    If you don't put "local", does the function apply to all of the scripts or just the script that you put the function in?
    Thanks!

  • @ShepWasTaken
    @ShepWasTaken 2 года назад

    so local in a function when you were explaining is basically like one town knows about this thing and the other does not but when you remove local its not just it that town anymore its not local anymore so the other functions or towns know about it. you made it very easy to understand tysm

  • @mimat_
    @mimat_ 4 года назад +21

    Test2: what the heck is a
    me: you just said what that has, an A in it.
    *sarcasm and yes my English is bad*

  • @bhaskarsingh1564
    @bhaskarsingh1564 4 года назад +2

    So the return basically transforms your function a value that your function will become if it becomes a variable.
    eg;
    local function lol()
    print("hi")
    return "hi"
    end
    Then you have to make the function a variable by;
    local trump = lol()
    then if you print the variable
    print(trump)
    It would give value you assigned to return.

  • @SuperBenBoy5886
    @SuperBenBoy5886 7 месяцев назад +3

    I still can't understand returning, even after looking at the comments and rewatching this 😞

    • @Marciunus16
      @Marciunus16 7 месяцев назад

      Returning is basically clearing the whole function and only left (return) a variable.
      SO run all of the code I have in the function, and when it's done. return "DONE" and forget all of the values we've calculated /printed anything:
      *local function example ()*
      *--blah blah*
      *--blah blah*
      *return "DONE"*
      *Local Helper = example ()*
      *print (Helper)*
      It's gonna print "DONE" because thats the only thing that was left in the function after it got cleared out. and then we made the *local Helper* be equal to the string "DONE"

    • @SuperBenBoy5886
      @SuperBenBoy5886 7 месяцев назад

      @@Marciunus16 So it basically deletes everything else that's in a function...? I don't know why I'd want to do that. I know there's probably some niche it fulfills but I have zero idea on what that would be since all of the tutorials I've been viewing for the past 2 years only consist of people messing around with simple variables and print statements instead of showing ways you can REALISTICALLY use them in the studio's 3D environment.

    • @Marciunus16
      @Marciunus16 7 месяцев назад

      ​​​​​@@SuperBenBoy5886Clear/delete is not the same. Every time a function is 'activated' it can learn new things again by reading lines,
      But using return we can remember and use a specific value IN EVERYWHERE IN OUR CODE.
      For example:
      Local function Calculate ()
      Local a = 4
      Local b = 5
      Local x = a*b
      Return x
      End
      Local helper = Calculate ()
      Print helper
      What will be the output of helper?
      20 because the function calculates and return x once it's completes. A and b are cleared no longer store memory of our pc and x = 20 value is saved because of return. Hope you understand tell me if I made any errors😅

    • @SuperBenBoy5886
      @SuperBenBoy5886 7 месяцев назад

      @@Marciunus16 so the "a" and "b" values no longer exist? I think I understand how it works, but my problem still stands in that I don't know how to utilize this in Roblox's 3d environment.

    • @Marciunus16
      @Marciunus16 7 месяцев назад

      @@SuperBenBoy5886 Exactly they don't exist until the line of code is read again. These small numbers are not even close to take up lots of storage but when you are making a big game (outside roblox) functions matter as the basics.
      It's not that hard to use.
      I'm not even good at programmig yet I use functions all the time.
      It just stores our code and we can recall it again anytime.

  • @vikovitch
    @vikovitch 2 года назад

    i attempted to learn vscript before this and it gave some nice base knowledge of return and other things

  • @hunaidkiani6100
    @hunaidkiani6100 Год назад +3

    Guys, what is the use of scope in scripting?

  • @Titan-qi9mv
    @Titan-qi9mv 4 года назад

    Niceeee when i was watching alvinblox returning video, i was like wtf im confused but when i watch your vid, i instantly know everything wow ty

  • @kur4130
    @kur4130 5 лет назад +5

    i need to watch all parts Beacuse started Making horror and I think ,"I have idea Lets make powerbox will turning off, on lights next think: No beacuse I don't know how to script I now Learned some script 😀^o^

  • @Parod1es
    @Parod1es Год назад

    This is still useful in 2023. I currently am a developer for tons of other syntaxes, so things made more sense to me, but still, this tutorial series is very helpful

  • @suaive2737
    @suaive2737 3 года назад +3

    2:03
    This guy calls himself an idiot

  • @janinemarquezjm
    @janinemarquezjm 3 года назад

    I needed more help for scripting i will watch ALL of your scripting tutorials for now

  • @Big_Shaq_Man
    @Big_Shaq_Man 9 месяцев назад +1

    Clarification for scope, if local is in an indent or something like that it shows only for that but if it is not indent or in indent it will show to everyone.

    • @Hyde_Tolut
      @Hyde_Tolut 8 месяцев назад +1

      I was so confused with that part, why the second function suddenly know what "a" equal to after he delete the local part, my dumb ass thought it know what a=2 is because the second function memorize it from previous run
      Turns out it know a=2 because its not local anymore