#about tables

1 messages · Page 1 of 1 (latest)

static bolt
#

okay i know how tables work and stuff but i was wondering how they could be applied to certain game mechanics, they're probably useful for shops, but how else are they useful.

I'm asking because i feel like I'll have to use it for a script I'm making (I'm a beginner and started scripting at the beginning of April)

young iron
#

for storing multiple values

static bolt
#

im kind of just trying to start up some discussion to get some coding help

#

okay cool

#

so how i could use it is for seperate unique blocks

#

so like

dirt block
mining time = 0.5

steel block

#

mining time = 10

young iron
#

for example you want to make a shop system so for the shop it needs a value inside
return {
["Sword"] = {
["Cost"] = 15,
["Description"] = "Good for close range combat :)"
},
} then you can use this value to make a template so you dont have to add a template one by one because it will automatically create a template by how many is in your table.. 🙂

#

i used table a lot so its only my experience

native stirrup
#

When working with tables, there are two different variants.

Arrays, which are stored values whos keys are paired numbers:

{1 = true, 2 = false, 3 = 10, ...}

Dictionaries, which @young iron was showing you, are values that have the index as a "key". These keys can be almost anything

{["Cost"] = 15, {"Descriiption"] = "HELLO"}
#

It just takes experience to use tables tbh

#

The more you use them, the more uses you find for them

native stirrup
young iron
native stirrup
#

Or use them as a lobby system which stores players in the lobby and other types of data

native stirrup
static bolt
#

okay so tables would be useless for what im trying to code

i want this system (and any future ones) to be applied to any future blocks i make

young iron
native stirrup
#

But yeah @young iron is right, you can use metatables to replicate OOP

static bolt
native stirrup
#

It's a programming structure to create recreatatble objects

#

For example, a animal class (apart of oop) can create an animal based off of criteria that you add

young iron
#

class is a blueprint where you can create multiple object by using a blue print and using that object you can use the method inside of the object or even use the methods of a blue print 🙂 that's how i interpreted it 🙂

#

methods is like an action by the way

native stirrup
#
local AnimalClass = {}
AnimalClass.__index = AnimalClass --Metatable stuff

type animal = ... --Custom Class Type

function AnimalClass.new(AnimalName: string, attributes: {}): animal
    local self = setmetatable({}, AnimalClass)
    ...

    
    return self
end
#

tbh I'm kinda lazy to finish this

young iron
#

hey idk what is type, export does?

native stirrup
native stirrup
#

So you can use that type for other scripts

young iron
#

wait how do i use export type tho.. outside of a module?

#

can you givem e some example

native stirrup
#

And if you require that module that had the exported type

#

you can call that type inside the new script

young iron
#

idk how type annotation works aswell because i only understand it only accept specific value like name: string

native stirrup
#
local TestClass = require(...)

type Animal = TestClass.animal
static bolt
#

okay how exactly could i make the little hoverbox system (when curser goes over block an outline appears) apply to any block in my game and any block named differently?

#

ive made it so it only works for the specific block ive made as of now

#

i feel like what i learn from this will be very useful for alot of things about code in the future (im a noob so im trying to learn all the different things and how i can apply them as game mechanics)

young iron
#

like this one 🙂 i dont want a long script inside of a local script that's why i added a module script to make less code in the future.

static bolt
#

YOU CAN USE CODE IN MODULE SCRIPTS FOR OTHER SCRIPTS

#

my near code inept mind is blown

#

its the answer to alot of my problems

young iron
#

you can organize your scripts using module script 🙂

#

i think that's my only purpose of using module script

native stirrup
#

@static bolt If you know how return's work, then modules should make perfect sense

#

You can only get data from a module that has been returned

young iron
#

i think to get into level 2 scripting i need to learn the best practices for um namings, and other stuff that improves readability of code right?

static bolt
young iron
#
    return n1 + n2
end

print(add(1,2))```
native stirrup
static bolt
#

idk if anyone will still be able to help with this but is there something about the mouse hover statements that are broken, because sometimes when i stop having the mouse hover, the outline on the block remains.

also when i made it play a sound it plays the sound a whole lot even though it in the middle of hovering, I'm assuming these bugs are happening because the game identifies my mouse as hovering over it multiple times (even if its already over the block).

#

you see it around the end of the video

#

i think i will repost this on a new post

#

ignore this i guess

native stirrup
#

For the last instance that was hovered over