#how to reference a table using the colon syntax

120 messages · Page 1 of 1 (latest)

subtle galleon
#
local t: table --table isnt a thing??
tame matrix
#

I could not find it either ig you can do smn like this (Not sure if this is good)

type tbl = {}
local testTable: tbl = {
  -- Table ig?
}; 
subtle galleon
lethal apex
#

like ```lua
type table = {any?}

subtle galleon
#

also could u explain the "?"

lethal apex
#

that checks if it is nil

#

not sure if that is already included in any tho

subtle galleon
#

ok

lethal apex
#

what do you mean "free" tables? empty tables?

subtle galleon
#

yeah

#

like anything that is a table

#

no matter whats inside it

#

cuz i dont wanna type everything i need

lethal apex
#
type tableType = {any?}
local t : tableType = {121, 123, a = 2, "asd", c = 123} --these are just examples
local t2 : tableType = {}
``` this worked for me?
#

unless you are talking about something else

forest willow
# subtle galleon ```lua local t: table --table isnt a thing?? ```

not sure, but heres the table type:

--defining
type table = {maxn: (t: table) -> number, find: (t: table, value: any, init: number?) -> number?, foreach: (t: table, f: (any, any) -> ()) -> (), foreachi: (t: table, f: (number, any) -> ()) -> (), getn: (t: table) -> number, concat: (t: table, sep: string?, i: number?, j: number?) -> string, insert: ((t: table, value: any) -> ()) & ((t: table, pos: number, value: any) -> ()), move: (a1: table, f: number, e: number, t: number, a2: table?) -> (), pack: (...any) -> {n: number, [number]: any}, remove: (t: table, pos: number?) -> any?, sort: (t: table, comp: ((any, any) -> boolean)?) -> (), unpack: (t: table, i: number?, j: number?) -> ...any, clear: (t: table) -> (), clone: (t: table) -> table, create: (count: number, value: any?) -> table, freeze: (t: table) -> table, isfrozen: (t: table) -> boolean}
forest willow
#

no

#

roblox-lsp gave it to me

subtle galleon
#

ok nice

lethal apex
forest willow
#

roblox doesnt consider "table" a type idk why

#

i had that issue before

#

but you can use
{any}

#

or, if you want to be more specific you can do:

type Something = {
  [number]: any
}
subtle galleon
forest willow
#

and didnt specify any element

subtle galleon
forest willow
#

for instance, roblox does this automatic for you

local y: {any} --[[empty table, can contain value with any type]] = {
  x = 3
}

-- roblox basically does this:
local y: {x: number} = { x = 3 }
forest willow
#

here the same error

#

you can fix it by doing

#

@subtle galleon

subtle galleon
#

yeah ik that but isnt there a way to not having to reference all the keys i need in the table

forest willow
#

yes

#

dont use strict or type

subtle galleon
#

whats stricy

forest willow
#

and let luau do it for you

subtle galleon
#

strict

forest willow
#

this

subtle galleon
#

im not using it

forest willow
#

are you on vscode?

subtle galleon
#

no

forest willow
#

it should not warn

subtle galleon
#

wth

forest willow
#

look at the first line of your script

#

if it has a --!strict

#

you can remove it

subtle galleon
#

trust me

#

theres no strict

forest willow
#

then just remove the type

subtle galleon
#

i didnt make it

#

theres no type

#

i just did as u did in the screenshot

forest willow
#

hm

#

did you try using table?

subtle galleon
#

yeah its not a thing

#

(idk why)

forest willow
#

weird...

forest willow
#

?

subtle galleon
#

yeah

#

how am i gonna do it..

#

if i stop hovering on it

#

it goes away

lethal apex
#

just write it out

forest willow
#

windows + shift + s

subtle galleon
forest willow
#

show me the function parameter

subtle galleon
forest willow
subtle galleon
#

what

forest willow
#

remove the {any}

#

and it stops the warn

subtle galleon
#

..

#

i would have not made this thread

#

if i were just gonna remove it

#

like i want to use it

#

but ig i will not

forest willow
#

hm

#

there must be a --!strict somewhere

#

it shouldnt warn you by doing this

subtle galleon
#

i swear

#

theres no strict

#

no weird stuff

#

just vanilla roblox studio

forest willow
#

try pressing
ctrl + f and search for --!strict

subtle galleon
#

im telling u

#

yk what fuck this

#

thanks for the help anyways

forest willow
#

np

#

gl

subtle galleon
#

thanks

subtle galleon
#

ok i fucking did it it was really simple

type dictionary = {[any]: any}
tame matrix
#

Oh yeah forgot about that

#

You could also do ```lua
type Array<T> = {[number]: T}

local TestArray: Array<number> = {
1,2,3,4,56,6,7,8,67,745,6,34,53
};

table.foreach(TestArray, print)

#

If you want to be fancy >:)

subtle galleon
#

nah im good

tame matrix
#

yeah

#

I would not do that either lmfao

subtle galleon
#

i have another question, if a paramater of a function has to be a model(or just any instance) and i want to get another instance from inside it, i get a warn saying the instance is not a key of that class, example:

function halflife(thing: Model)
  thing.Humanoid.Health /= 2 --Key 'Humanoid' not found in class 'Model'
end
#

is there a way to fix this

tame matrix
#

I would also like to know

subtle galleon
#

also fun fact when u reference what a function returns but it doesnt return anything in the function block it warns, even using the "?" operator after the type :

function check(x, y): boolean? --even using "?" it warns
  if x == y then
    return true
  else
    return false
  end
end --theres a little warn here cuz nothing is actually returned inside the function block but only inside the if block

u can easily fix this by just adding an empty return at the end of the function:

function check(x, y): boolean?
  if x == y then
    return true
  else
    return false
  end

  return
end --doesnt warn anymore
subtle galleon
#

can someone explain <> or send me a link to an explanation

lethal apex
#

like
type hashtable <ARG> = {[ARG] : boolean}

subtle galleon
lethal apex
#

that was the example?

#

local Players : hashtable <Player> = {plr1 = true, …}

#

thats what it looks like used