#How do I count the number of indexes in this table?

85 messages · Page 1 of 1 (latest)

fallen spade
#

So i have a table:

/* eslint-disable prettier/prettier */

interface TowerLibraryInterface {
    [key: string]: {
        WalkSpeed: number;
        Scale: number;
        Health: number;
        WaveSpawnRequirement: number;
    },
}

const TowerLibrary: TowerLibraryInterface  = {
    Normal: {
        WalkSpeed: 16,
        Scale: 1,
        Health: 20,
        WaveSpawnRequirement: 1
    },

    Zombie: {
        WalkSpeed: 10,
        Scale: 1.5,
        Health: 40,
        WaveSpawnRequirement: 3
    },

    SpeedyZombie: {
        WalkSpeed: 32,
        Scale: 0.5,
        Health: 5,
        WaveSpawnRequirement: 5
    },
}

export default TowerLibrary```

and I want to count the number of infexes in that table in another script, here's what the other script looks like:

```typescript
import TowerLibrary from "./TowerLibrary";

export function startWave(currentWave: number, enemysToSpawn: number) {
    const mobListToSpawn = {}
    const unitsToChooseFrom = 3 // What im looking for is something like (TowerLibrary.size() <-- to return the number of indexes) but i can't do that
    let enemysLogged = 0
    while (enemysLogged < enemysToSpawn) {
        // Preferbaly i want to count the amount of units in the "unitsToChooseFrom" const so I can do the following:
        let randomEnemyName = TowerLibrary[math.random(unitsToChooseFrom)].Name

    }
}```

can someone wrtie a way for me to do that? so I can learn it thank you 🙂
#

id also want someoen to write the part where i can put the random dictionary into the mobListToSpawn as well

#

update i made a count index function

#

but if there's a better way id like to know

#

nvm no id idnt cause that snot a thing apprently

fallen spade
feral elk
#

oh?

fallen spade
#

object is considered a type

#

not a value

#

for me

#

im using VSC

feral elk
#

what does the hover message say

fallen spade
#
export function countIndexesInTable(any: {}) {
    let count = 0;
    for (const key of any) {
      count += 1
    }
    return count;
}```
#

so now im trying and failing at this

feral elk
#

but unless it's deleted, Object should always be a value

fallen spade
#

for me its of, in isn't supported

#

for trasnformation

#

its roblox-ts

feral elk
#

ohh

fallen spade
#

yea

#

i just need to acquire a way of counting indexes

feral elk
#

does roblox ts have pairs

fallen spade
#

no

#

you dont need it

feral elk
#

oh

#

weird, why is of any failing then

fallen spade
#

says it neds a .iterator operatior

#

or whatever

feral elk
#

yes

fallen spade
#

it says {} needs to have a .iterator

feral elk
#

that's what pairs is for

fallen spade
#

lemme check if it has pairs

#

then

#

do i do pairs(any)

#

i didnt need it b4

#

oh

feral elk
fallen spade
#

worked now

feral elk
#

what did u have before

fallen spade
#

uhhh

#
**import { ReplicatedStorage, Workspace } from "@rbxts/services";

const stagesToChooseFrom = ReplicatedStorage.WaitForChild("StagesToChooseFrom").GetChildren()
const obbyStagesFolder = Workspace.WaitForChild("Obby").WaitForChild("Stages") as Folder;
const locationsToRenderStages = obbyStagesFolder.WaitForChild("RenderAt").GetChildren();
const existingStages = obbyStagesFolder.WaitForChild("ActualStage") as Folder;

function generateNewStages() {
    for (const child of locationsToRenderStages) {
        const renderLocation = child as Model
        const randomStage = stagesToChooseFrom[math.random(stagesToChooseFrom.size())] as Model;
        const newStage = randomStage.Clone() as Model

        newStage.PivotTo(renderLocation.GetPivot())
        newStage.Parent = existingStages
    }
}

export function makeNewStages() {
    existingStages.ClearAllChildren();
    generateNewStages()
}```
#

for example in this scenario i didnt need it

#

to generate new stages

#

which is why i was confused

feral elk
#

yes, of works out of the box for arrays

#

not for object

#

wait

fallen spade
#

im pointing out me not having to use pairs()

fallen spade
#

well yeah you dont have all of the qualifications

feral elk
#

unless luau is weird

fallen spade
#

^

#

you have to understand both

#

for example luau has a :GetChildren()

#

in ts its .GetChildren()

feral elk
#

no no no

fallen spade
#

it takes the children of an object and compiles it into a table

#

oh

feral elk
#

the issue is that luau is way different from regular lua 5.1

#

seems like luau has __iter which lets you iterate over... regular arrays?

fallen spade
#

oh yeah

#

it calls it automatically

#

without the need to call it manually

feral elk
#

also it has += which lua 5.1 doesnt have either

fallen spade
#

didnt know that

#

lol

#

why do people read docs btw

#

whats the point

#

why dont u just learn whats relevant

#

to your use case

#

like whne it comes to ts im not gonna learn anything unless i run into a need fo rit

feral elk
#

thats what i do tho

fallen spade
#

oh

#

ok coo

#

question do u make lots of money*

feral elk
#

umm

#

idk

fallen spade
#

do u make 150$ a day

#

im just asking cause i know people whod pay like 20-30k usd for a game in roblox, would take u like a week to complete it