#how to pass a table from a different function

1 messages · Page 1 of 1 (latest)

prisma charm
#
event:onServerEvent(function(player, event, pos)

  local function createframes()
    local framelist = {}

    for i = 1, 64 do
      local randomRarity = math.random(1, 100)
      local CommonItems = ServerStorage.CrateFrames.Common:GetChildren()
      local RareItems = ServerStorage.CrateFrames.Rare:GetChildren()
      if randomRarity == 1 then
        local randomRare = RareItems[math.random(1, #RareItems)]:Clone()
        framelist[i] = randomRare.Name
      elseif randomRarity > 1 then
        local randomCommon = CommonItems[math.random(1, #CommonItems)]:Clone()
        framelist[i] = randomCommon.Name
      end
    end
    return framelist
   end

  local function stopfunction()
    local frametime = (math.floor(pos/0.25) - 34) * (-1)
    print(frametime)
    local winner = framelist[frametime]
  end
    
  if event == start then
    createframes() -- creates framelist
    
  elseif event == stop then
    stopfunction(pos) -- needs the table that was just created... how do i pass it to here???
  end
end)

Basically this is a stripped down version of the code
i create a table called framelist in the createframes() function
then i also want to use that table in the stopfunction() function aswell
how do i pass the table over to the stop function?

random lava
#

Is the table an array?

prisma charm
#

yes

#

just a list of itemnames

random lava
#

If so cant you put it in the parameters?

#

You could also do: local table

#

And edit the value later

#

Like: table = 3

prisma charm
#

i will just say im very new to scripting (around 6 months)
i found tables hard to understand at first and i would just getchildren of folders full of items instead so im trying to work them out now
how do i put it into the parameters? cuz its created outside the scope

random lava
#

I cant give a full explanation

prisma charm
#

createframes() is called outside the scope of stop function

#

its all goods i might of done this wrong i think

random lava
#

If you have the table then you can do table[1] to acces the first thing in the table

random lava
prisma charm
#

not yet

#

i dont know how to do this correctlyh

random lava
#

Could you send a ss of the code?

prisma charm
#

the code is there?

random lava
#

Its hard to read on mobile

prisma charm
#

ahh

#

nah take ur time dude

#

here ill take screeny

random lava
#

Ty

prisma charm
#

i might sstrip down the code a bit more i still have redundant stuff in there hold on

random lava
#

Alr so

#

Redneck solution: add this below the line that finishes the table: local array

#

No way

#

Wait

#

Do that at the top

#

Then set array = (your table)

#

And then you can acces the table anywhere

#

As long as the line is run first

prisma charm
#

for some reason its empty even after i run createframes()

#

ill try putting it completely outside the whole event:onserverevent function

random lava
#

Alr that means that your table is not created until you actually need it

#

Theres a better way to do this 1 sec

prisma charm
#

heres a even simpler screenshot without the other parts

random lava
#

Give the name of the variable you want to acces

prisma charm
#

framelist

#

let me try setting it outsoide the scope rq

random lava
#

Above the function add: local array

#

Then when event == start do: array = Function()

#

Bc it returns it it should automatically set the variable to the return value

#

Wait

#

Oml

#

Just add local framelist above the function ;-;

#

Instead of in it

prisma charm
#

i did look

#

still gives empty array

#

okk sorry my code a little different i have print( framelist)

random lava
#

Above the local function createframe()?

prisma charm
#

yes

#

ill send actual code hold on

#

its a bit more cluttered tho

#

so bear with it

#

ill send a screenshot

random lava
#

Thx would be easier for me

prisma charm
#

hold on i had been testing a lot let me make sure that it didnt fix it cuz i have framelist = createframes() where it calls the function lol

random lava
#

Idk where you print framelist ngl

#

Bc if you print it outside of the function then the list might not have been filled yet

prisma charm
#

here

#

that will make more sense

#

its called gameInit() not createFrames()

#

so thats a difference

#

i set framelist back to {} at the end to prevent exploiters trying to brute force items

#

but thats right at the end

#

shouldnt really mess with it tho

#

i know for sure the array works because if i print(framelist) within gameInit() it shows all the items

#

btw if you couldnt tell this is a script for a crate spin that the player press 'stop' instead of pure rng

random lava
#

Mb waa afk for a min

prisma charm
#

you're all goods brother take ur time im glad that you're helping me

random lava
#

Can i ask where you call the functions?

prisma charm
#

i can easily just do rollframe:getchildren() but i want to learn tables

random lava
#

I get that

#

Honestly a table is the same as a folder ngl

prisma charm
#

yeah its way down at the bottom hold on - i also have a count function that counts down from 5

random lava
#

Can you send that too?

prisma charm
#

you can see i tried even setting framelist = gameInit()

#

ill try without that too

#

still same result

random lava
#

This is kidna tough

prisma charm
#

it is a bit

#

i thought about module scripts but with multiple players trying to run a crate at the same time i think will start bugging the game unless i set player.userid's etc then it gets too complex

random lava
#

If i had to guess you fired the stop function before the listfuction

#

But im really unsure

#

And i gtg in 2 min

prisma charm
#

no i just coded it out of order which shouldnt matter it runs gameInit First

#

and all goods dude i can just use folders for now

#

ill figure it out another day

#

rollframe:getchildren()

random lava
#

Alr if you wanna practice then make a script that keeps track of all the players that joined in a table

#

Gl man 👍