Whats the difference between Tables and Values?

I just started to script and im someone who likes to use values and numbers, but i noticed that some people is using Tables, i found that they are like values but something is different, so i want to say what’s the difference between both? Thanks

A table is just a value that can hold multiple other values. I’m not sure what your definition of value is if it doesn’t include tables, so I don’t really know what kind of answer you’re looking for.

1 Like

A table is like a collection of data you can easily access within the script
an iterate through.

to define a table :

local my_table = {        
 "name1" , "name2" , "name3"
 }
or you could do this (click here)
local table = {
"one"; 
"two" ;
 "three"
}

to iterate over values in a table do something like :

local textlabel = script.Parent.TextLabel
 --we are trying to change the text, concatinating each with the value, for _,v
local my_table = {
	["one"] = "one thing";
	["two"] = "two things";
	["three"] = "three things";
	["four"] = "four things";
	["hello"] = "my_message"
}
 
while  true do
	for number, description in pairs(my_table) do
		textlabel.Text = "The number is " .. number.. "! " .. description
		wait(1)
	end
end

for example if we want to do something for a certain player, if his name is found in the table then…

 playersnames = {--for demonstration
		"name1";
        "name2";
	}
	
	player = game.Players.LocalPlayer
	if player.Name  == table.find(playersnames,player.Name) 
	then do print("something else")
although this could alternatively be done by looping , like : (click to view)
local players = game:GetService("Players")
local Allplayers = players:GetPlayers()
for _,loopedplayer in pairs (Allplayers) do
	if loopedplayer.Name=="name"
	then 
		local tool = 	game.ReplicatedStorage.tool
		 tool.Parent=loopedplayer.Backpack--give this guy the tool
	end
1 Like

Wordy explanation.

Tables are a blanket term for two types of collections: arrays and dictionaries. Values are simply pieces of data which are held somewhere or represented by a variable.

An array is a collection of items where your values are in order of how they’re placed. They have an implicit index based on their position. The table library works with arrays the best.

An array is represented as so:

local array = {1, 2, 3}

As for a dictionary, it is a type of table where you define both the index and the value. This is helpful if you want to retrieve a value out of a DataStore by association. It is represented as th following:

local dictionary = {
    ["A"] = 1,
    ["B"] = 2,
    ["C"] = 3
}

Tables alike can be split with either commas or semicolons. If you want to add items from outside the table, you simply need to assign a value to a variable. How you do so depends on what you’re using.

-- For arrays
table.insert(array, 4)

-- For dictionaries: two methods
dictionary.D = "4"
dictionary["D"] = "4"

You are able to have a table where both array and dictionary elements are present. This is called a mixed table. Beware when using mixed tables, as this could trip you up for several occasions, especially when working with remotes and DataStores.

A common use for tables will be when you want to collect an assortment of items and iterate over them. To iterate means to perform an action for each item of the table. Iteration is done with a for loop. There are two types: numeric and generic.

A numeric for loop will run across a certain value you supply. It is done in the format i = startNum, endNum, increment. i is a variable definition for your loop variable. It will begin at the number you assign it, run until the second number and increase by the third. The third number can be positive or negative, greater than one or less than one. It is optional and will default to 1 if not used.

for i = 1, 10 do
    print(i)
end

A generic for loop is used to iterate over a table given a function to use that is responsible for searching for the next result. It is in the format for i, v in generator. i and v are variables local to your loop that represent the index and the value of it. Generator is the iteration function. There are two that are provided to you as defaults: pairs and ipairs. Careful which one you use: it actually does matter, especially if you hope to understand the difference.

ipairs is designed to iterate over arrays. It will start at one and incrementally (increasingly) return values in guaranteed order. It works somewhat like the numeric for loop but you get direct access to your indexes and values instead of needing to do array[i] in the numeric for loop. This is helpful for things that return arrays, such as GetChildren.

pairs is designed to work with dictionaries. Because your indexes aren’t in numeric order and are unknown (even if you use numbers or assign keys by ascending numbers, it will not be an array), it is designed to get the next index based off of the last one and return both the index and the value. Unlike ipairs, it takes unnoticeably longer because it doesn’t automatically know what your next index is. It also will not return values in order, it will attempt best fit.

If you’d like more information, be sure to check out the Developer Hub or Lua website. For values, search up “attribute-value pairs” on Wikipedia for the overall concept of values. While arrays and dictionaries can be found in other languages, it’s best you understand them based on what language they’re for since some languages implement and write them differently.

2 Likes

However if you would want to return a function based on a criteria, for instance you would like to give a player whose name matches the defined values , a tool
then you would do something like :

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function()--everytime a player joins
	
--function

local guys = Players:GetPlayers()--to be iterated through

local PlayerIds = {
	--specific people, to "match" with
	451391631;--[[xxelectrofusionxx 's id, its recommended to use ids instead of usernames)]]
	6809102--some other great guy
	
	}


for _,guy in pairs(guys) do--through children
	if guy ~= nil then 
	if not table.find(PlayerIds,guy.UserId) then return end--[[You're user id doesn't match? let's end it right here]]
		 print(guy.Name.."has joined the server, this works !!!")--otherwise
local tool = game.ServerStorage.tool:Clone()
tool.Parent = guy.Backpack
	   --function
  
	   end
           end 
               end)--ideally this script would be in `ServerScriptService`
1 Like