[Module] Better Stack

Hey there developers!

Here’s a new and a very useful module I created named Better Stack. This is a module created in Lua for Roblox Studio. It expands the limits of a regular Lua stack which has more than 20 functions to use. New Functions are added frequently. It is completely Open Source!

If you don’t know what a Stack is -

A stack is a Last-In-First-Out (LIFO) data structure. It is very commonly used in computer programming. A stack can easily be imagined as a stack of dinner plates. You start with one, and then you put another on top. In order to get to the very first plate, you must remove the plate(s) on top of it. So you remove the last plate first . The act of putting a plate on top of the stack is called pushing while removing a plate from the top is called popping .

About

This is a module based on the data structure known as Stack. It has tons of functions you can use, whether it be connecting to stacks, pushing elements to a stack, popping elements from a stack or arithmetic operations. This module has it all!

Why Is There A Need To Use This Model?

Inside Roblox Studio the data structure Stack is not a thing. What I mean by this is there isn’t an inbuilt stack for you to use. In order to use a Stack you had to code it yourself. To make everyone’s task easier. I went out of my way to create this useful module! It has more than 20 functions! (New functions are added frequently) for you to use! Its not just a normal Stack. It has so many useful functions which will be listed down in the post in the Documentation section hence the name Better Stack

Important Links

Get the Module here
Github Repository

Instructions

To run the functions you must get the module here which has also been listen above in Important Links.

When you get the model. A script named Instructions has pop up in explorer. You must read it for the module to work.

  • Step 1 - Place the module script named “BetterStack” that is present inside the “Instructions” script, inside the Replicated Storage.

  • Step 2 - To start using the module. You’ll have to require it. Type the following code inside any script you want the Stack to be present in.

local BetterStack = game:GetService("ReplicatedStorage").BetterStack

That’s all! Enjoy using the module! The setup is complete. Now you can view the Documentation down below.

Documentation

All functions and code examples are listed below. Almost all these functions can be printed using print(). They can also be stored in variables!

Make sure to have this in your script!

local BS = require(game:GetService("ReplicatedStorage").BetterStack)

Declare a Stack | .new

local myStack = BS.new()

Push an element | :push()

myStack:push(10) --my stack is now 10 (Top element is 10)
myStack:push("Hello") --my stack is now 10, "Hello" (Top element is "Hello")

Pop an element | :pop()

myStack:pop() --my stack is now back to 10 (Print Function can also be used. The value can also be stored in other variables)

Push multiple elements to a stack | :pushMany()

myStack:pushMany(3,4,5,6,7,7,8,8) --adds all these elements to the stack at once

Top element of the Stack | :top()

myStack:top() --stores the top value in the stack

Bottom element of the Stack | :bottom()

myStack:bottom() --stores the bottom value in the stack

Delete a stack | :bomb()

myStack:bomb() --makes the stack empty

Size of the Stack | :size()

myStack:size() --size of the stack aka number of elements in the stack

Remove N number of elements from the top of the stack | :cut()

myStack:cut(2) --remove top 2 elements from the stack

Connect multiple stacks to a stack | :pile()

myStack:pile(stack2, stack3)

Print the stack at once | :print()

myStack:print() --prints the stack

Check if a value is present in a stack | :find()

myStack:find("Epic") --returns true if "Epic" is present in the stack, else returns false

Get minimum value in the stack | :min()

myStack:min() --gets minimum element from the stack (only possible with numbers)

Get maximum value in the stack | :max()

myStack:max() --gets maximum element from the stack (only possible with numbers)

Get average of all elements in the stack | :average()

myStack:average() --returns average of all elements in the stack (only possible with numbers)

Get middle element in stack | :mid()

myStack:mid() --returns middle element of the stack

Check if the stack is empty | :isEmpty()

myStack:isEmpty() --returns boolean value

Set a description about the stack | :desc()

local description = myStack:desc("This is a better stack!") --sets description of the stack provided in the argument

Check the most frequent values in the stack | :based()

myStack:based() --returns the most frequent values

Returns “String Based” if most of the values are Strings
Returns “Number Baed” if most of the values are Numbers
Returns “Boolean Based” if most of the values are Booleans
Returns “Multi Based” if multiple types of values are equally present in the stack.

Check frequency of an Element | :freq()

myStack:freq(4) --returns the frequency of the element "4"

Check if stack has a String value in it | :hasString()

myStack:hasString() --returns boolean value

Check if stack has a Number value in it | :hasNumber()

myStack:hasNumber() --returns boolean value

Check if stack has a boolean value in it | :hasBool()

myStack:hasBool() --returns boolean value

That is all for the documentation

Thank you for reading my post!
Hope the module helps you in your development!

If you would like to ask queries or give feedback be sure to reply down below!

How helpful did you find it from a scale of 1 to 5?
  • 1
  • 2
  • 3
  • 4
  • 5

0 voters

Thanks! :grinning:

9 Likes

There are a couple mistakes I found while going through the source. Nothing too big, but the behaviors wouldn’t be expected.

Tables are passed by reference, so :clone isn’t actually cloning, it is just returning itself.

:bomb returns an empty table, however this would create a new table, instead of just clearing the current stack.

There were a few more things that could use improvement, which I’ve suggested improvements to. I’ve made a pull request :slight_smile:

2 Likes

Ah, that made me laugh probably harder than it should. Anyway, amazing module! Stacks make my brain want to hurt sometimes, this makes it a lot easier.

One question: Is stack:top() meant to be a variable? So I would say

local top = stack:top()
print(top)
1 Like

Hey there!

Yes it is meant to be in a variable.

local top = stack:top() is correct!

print(top) or print(stack:top()) can be used as well!

Thanks

1 Like

Hey there!

Big thanks to you for finding the mistake! I have fixed it now. I have removed the :clone() function for some reasons as it wasn’t really needed. I have fixed the :bomb() which empties the stack.

Can you provide a usage of stacks? I don’t know what I’d use stacks for.

Here is what it is

Here is where you can certainly use it.

Stack is a Data Structure. It may not be used primarily in Game Development on Roblox but it is a key element of programming and scripting. Stacks are used vastly through programming. For example - Say you have something like tower which has different elements in it. And say that you want to get only the element at the top of the tower. You can add elements to the tower as well as take elements out of the tower using the principle of Last in First out. Stacks are mostly used in competitive programming or possibly solving algorithms or different aspects of your game. There is also a data structure known as a Queue which also has the same aspect of Stack but they comply with the principle of first in, first out. Stacks may not be used every time in development but can be really helpful in some cases!

2 Likes

I would like to make a suggestion if your gonna continue development on this, maybe a visual representation of what your module is used for, I feel this would be far more helpful then just an explanation (not to say your explanation is bad). It’s more like I don’t quite understand how it will save me time in anything if I have nothing to compare it to.

Thank you for your suggestion. I’ll take that into consideration and maybe in the future I’ll implement it!

What’s the point of this, though?

table.remove(t[, i]) returns the value that it removes, so you can trivially implement a Queue or Stack with it and table.insert(t[, i], v).