Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a Value that can hold tables?

Asked by
Discern 1007 Moderation Voter
9 years ago

So there's StringValues, which hold strings, IntValues, which hold Integers, etc...

But are there any type of values that hold Tables?

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

No, there is not.


You can store values in _G, the global table, an object that all Scripts have the same reference to, though these won't be replicated to clients (LocalScripts), or between them.

-- _G Example:

-- Script 1
while true do
    wait()
    _G.cat = {jumping = math.random(1,2) == 1}
end

-- Script 2

while not _G.cat do
    wait() -- Wait for the first Script to set up
end

while true do
    wait()
    print( _G.cat.jumping )
    -- randomly changing true and false printout
end

In general, using _G isn't very elegant of a solution.

One option is to substitute this with a reference provided by a ModuleScript.


Another way to do something equivalently is to use RemoteFunctions / BindableFunctions to provide an API to read arbitrary data, structured however you designed.




I want to somehow access the table of winners in another script

In that case, it would make most sense to just use a BindableFunction (or one of the other more or less equivalent ones, like RemoteFunction or BindableEvent)

-- BindableFunction
-- Script with `winners` table
function game.RequestWinners.OnInvoke()
    return winners;
end

-- Other script needing the list of winners currently

local theirWinners = game.RequestWinners:Invoke()
-- (Where game.RequestWinners is a BindableFunction)

Though passing this value between scripts probably isn't the best thing to be done.

Probably, you can manage in just the one script.

If things like GUIs or other things need to respond (e.g., "YOU WON!") you only need to write a value to the person and say their place (i.e., just an IntValue); the GUI reads this single value and responds accordingly.

If each player's GUI wants to show the list of winners, the main script should do the same thing as before. This time, the GUI script will simply consult all of the players instead of just the one.

If for some reason processing those numbers is complicated (perhaps odd tie-breaking rules), you can either

  • use a module of some sort (global function, ModuleScript) to do that work, that both the GUI and Game script can use
  • have the game compute the necessary values and give, in the same way, those values to the GUIs
0
My situation is that there are is a table of winners of a minigame in a script. The problem is, people can tie with the same amount of points accumulated in the game, so I have to insert the people who tied into the table of winners. I want to somehow access this table in another script. What would be the best solution? Discern 1007 — 9y
0
I'm going to figure this out on my own, but I'll accept your answer because you gave me some knowledge about this, and I learned a lot. Thanks. :) Discern 1007 — 9y
Ad

Answer this question