CVExtra -- Super Module Script (DEVELOPMENT FORUM)


  • @Cvieyra2test Although there's been constant explanations why to use local variables, I'll give my go.

    1. If you don't use local variables for their scopes, variables will be rewritten.

    For example, image if a player were to increase their cash every 1 second. The following'll overwrite.

    function GiveCash(Player)
         Cash = Instance.new('Cash')
         Cash.Name = 'Cash'
         Cash.Parent = Player
         while true do
              wait(1)
              Cash.Value = Cash.Value + 1
         end
    end
    

    If another player were to join, it'd increase his double, then for the next, triple, etc.

    1. Memory Leaks (three links in one, depending where you click ;) ) will occur.

    Put simply; if it doesn't end somewhere, memory's going to build.

    .

    Off-topic; you completely disregarded what I'd stated previously. Stop. Pushing. It. On. Posting what you just did completely stabbed what I'd said before.

    TL;DR It's over. Quit beating at a dead horse. Don't even bother responding to this part of my post. Move on.


  • @sjr04alt It's faster to access?

    @TheAlphaStigma Well I'd never use it in a function like that, so I definitely know about that. I didn't see that much of a point to adding local's when given this example.

    function GiveCash(Player)
         Cash = Instance.new('Cash')
         Cash.Name = 'Cash'
         Cash.Parent = Player
         while true do
              wait(1)
              Cash.Value = Cash.Value + 1
         end
    end
    

    Also, I do know about it being local to the scope itself, which wasn't the purpose of what I had stated, it was in the global scope of the module script.

    Thank you for providing the links. Now I do see more to why I should. As to better memory management.

    Off-topic: I don't get how I have disregarded anything you have said? As though I will state, it's not a learning experience until I have understood how it applies to what I'm doing, thus why I criticise the criticism. It's something that is simply required.

    I do not see how the "example" had a lot of fluff? The pcalls were used purposefully as to avoid security checks. It may seem like an "if" statement mess.


  • @Cvieyra2test here's a tip

    use

    pcall(fn, ...);
    

    rather than

    pcall(function(...)
        fn(...);
    end, ...);
    

    So use pcall(Instance.new, className);

    If pcall returns true as first return, everything else after the first return is the returns of the function.

    Here's an example:

    local success, object = pcall(Instance.new, "Part");
    

  • @sjr04alt I've seen a familiar format before, so that actually works like that? Thank you for the tip.

    Dang wiki never tells you anythin'

    Been also trying to find a function that'd print out red text without ending the program flow.

    assert(), error(), TestService:Error() are all I found, when I wrap them in a pcall, it doesn't print out anything. But then again, it is pcall and it catches errors...


  • @Cvieyra2test, the wiki is trash. I never recommend to learn anything from it. Its only use is finding out the name of a method you forgot. Maybe you should study Lua5.1 in more depth.

    pcall(func, ...)
    

    should be a commonly known fact.


  • @Phlegethon5778 I cannot disagree, that wiki is quite trash, I never go to it for scripting help. But as you stated, rather to figure out that function I forgot.

    But now it doesn't even teach ya about base, global functions aaaa

    But I'll take your word for it to look up the Lua 5.1


  • @Cvieyra2test, I recommend the reference manual. It's helped me immensely. Some parts are confusing and hard to understand, and you'd probably never encounter most of them on Roblox, so you can ignore those parts.


  • Chapter two is probably going to be the most helpful to you.


  • @Cvieyra2test @Phlegethon5778

    Roblox Wiki for learning the Roblox API

    Lua official site for learning Lua

    I didn't know you can use string patterns to get information about the date. For example, os.date("%x") returns the date in string format MM/DD/YYYY.

    In string patterns %x is a hexadecimal. The wiki doesn't document that you can use this for os.date


  • Questions:

    How do I create a function like pairs() ? (I wish to make a function that can iterate through a userdata/table's metatable)

    Nevermind about the above, this article (https://www.lua.org/pil/7.3.html) just helped with that

    How do I convert the PlaybackSpeed property of Sound instances to musical notes?


  • @Cvieyra2test you don't, use playbackloudness instead lol


  • @Cvieyra2test You could use Game Hero's Midi Player as a reference. :p


  • @Fifkee reee that's how I'd do cool visual things like https://www.roblox.com/games/187659887/Audio-Visualizer

    @TheAlphaStigma Ooo thanks!

    The idea of me getting these notes 'n stuff is to create something actually similar to it.

    With PlayAudio, I plan to make a lengthy piece of audio that contains numerous instruments. And then, with those instruments, create something that people can make audio with. However I will certainly look into this Midi player for definite reference in both note definition and how I'd have my thing work out as well. I'm curious how they managed to do it on Studio, since you can't modify noises that are played in Roblox Studio, unless you can?


  • @Cvieyra2test

    How do I create a function like pairs() ? (I wish to make a function that can iterate through a userdata/table's metatable)

    pairs doesn't actually do much work other than return. It is literally written as

    function pairs(t)
        return next, t, nil
    end
    

    So when pairs is called on each iteration it's really next doing all the work.

    With this knowledge in mind you can get the metatable like this

    local function traverseMetatable(object)
        local mt = getmetatable(object);
    
        if (rawequal(type(mt), "table")) then --// there is a metatable
            return next, mt, nil;
        end
    end
    

  • Had a feeling it had to do something with next, just didn't know how to apply next to it. Thank ye' although


  • This post is deleted!

  • @sjr04Alt However, question, what is the third argument you have provided there,
    "nil"?

    It is known as "0" on the Lua wiki for ipairs, is it like the starting position or somethin'?


  • @Cvieyra2test I believe he's referring to this, where it reads

    The pairs function, which iterates over all elements in a table, is similar, except that the iterator function is the next function, which is a primitive function in Lua:
    
        function pairs (t)
          return next, t, nil
        end
    

  • 0_1555295049328_upload-ddab7055-8803-478d-97c4-f499171114af
    It finds the metatable, knows both the length and the stuff in it.

    But... It doesn't return anything with the return statement???

    It prints out an empty value in output.


  • @Cvieyra2test said in CVExtra -- Super Module Script (DEVELOPMENT FORUM):

    However, question, what is the third argument you have provided there,
    "nil"?
    It is known as "0" on the Lua wiki for ipairs, is it like the starting position or somethin'?

    ipairs is for traversing the array part of a table. It returns 0 since it adds 1 and Lua's arrays start at 1.

    @Phlegethon5778

    you use rawequal in too many unnecessary places. There is very rarely a need for it.

    To bypass the __eq metamethod. It's just a new habit of mine. Semicolons and my conditional expressions in parenthesis is also unnecessary but you don't complain about that?

    What if the metatable has its own metatable which has an __eq field? Hmm?

    @Cvieyra2test (again)

    You would not use it like this. I'll quote the PIL:

    The call next(t, k), where k is a key of the table t, returns a next key in the table, in an arbitrary order. (It returns also the value associated with that key, as a second return value.) The call next(t, nil) returns a first pair. When there are no more pairs, next returns nil.

    You don't need to use nil, you can just return next, mt. But the PIL returns nil as well so why not

    For ipairs, I'll go in a bit more depth.

    In that link, the iter function is called passing the argument of 0. This is the i parameter. It is incremented by 1. The next call will be iter(a, 1) and will then be a[2], and so on, until a nil value is encountered.

    For pairs (again x2), that is also why some people put next directly instead of calling pairs. This adds a negligible performance boost. Use pairs. It's clearer. Micro optimisations are never a good argument. You rarely need to use next, both inside or outside a generic for loop.

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.