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

Could someone explain these odd table manipulation techniques? [closed]

Asked by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

In this question's answer, there are some lines of code I do not understand.

The asker (Bman) has informed me that the code provided by the answerer (Adark) was fully functional with no edits. Here is the entire code;

repeat wait() until game.Players.LocalPlayer.PlayerGui
----------------------------------------------------------
-- Disable roblox's dumb stuff
---------------------------------------------------------- 
game.StarterGui:SetCoreGuiEnabled("All", false)
----------------------------------------------------------
-- Global Variables
---------------------------------------------------------- 
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local sprite = script.Parent.Player

local keysDown = {}
----------------------------------------------------------
-- Movement
----------------------------------------------------------
game:GetService("RunService").RenderStepped:connect(function()
    local  move = 0
    if keysDown.a then move = move - 1 end
    if keysDown.d then move = move + 1 end  
    sprite.Position = sprite.Position + UDim2.new(0.01*move,0,0,0)
end)
----------------------------------------------------------
-- Keyboard input
---------------------------------------------------------- 
Mouse.KeyDown:connect(function(Key)
    keysDown[Key] = true
end)
Mouse.KeyUp:connect(function(Key)
    keysDown[Key] = false
end)

The lines I do not understand are keysDown[Key] = true and keysDown.a


keysDown[Key] = true

In tables, you can 'set' the value of a specific index by doing something like

local myTable = {}
myTable[1] = "Hello"

However, here there is no index. 'Key' is a string, not a location, so how can he set keysDown[Key] to true?


keysDown.a

Firstly, what is this? I'm used to dots only meaning children or properties, never tables. If it works the same way, however, it still does not make sense since there will never be anything in the table named 'a'.

Please be detailed in your answers.

1
It seems that when a key is pushed down it adds it to a table. Then when the renderstepped function plays, It checks to table to see if "a" or "d" are being pressed, and if one of them are it moves the sprite. Simple as that :) YasuYoshida 171 — 10y
2
I would appreciate an answer on the specific things I asked, and how they work; I've never seen them before. Perci1 4988 — 10y
1
Table keys are not only restricted to numbers, you can put almost any type of data as a key. The script is taking advantage of that so when they press a key, it sets the value to true where the location is actually a string. Read more here: http://lua-users.org/wiki/TablesTutorial jakedies 315 — 10y

Locked by Perci1, EzraNehemiah_TF2, Redbullusa, and BlueTaslem

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
2
Answered by
Bman8765 270 Moderation Voter
10 years ago

Okay, so I am almost positive I got this. It's not roblox wiki but it is a good example. Lua-Users has a very good article on the roblox tables. I enjoy reading over the code as it challenges you to learn. Basically using Table.variablename is just a literal shortcut to doing it For example:

Table = 
{
    hi = "hi"
}

print(Table["hi"])

Is the same as

Table = 
{
    hi = "hi"
}

print(Table.hi)

It is literally just a shortcut Lua allows you to do. Both work and you could use both either way but it just is more "traditional" for an "expert" scripter to use periods to define locations in Table Dictionaries.

keysDown[Key] = true is just adding a new variable to the table dictionary with the variable name being the value of key (in this case a string value) and then giving it the value of true.

Hopefully this helped, if it did make sure to accept the answer!

EDIT: To touch a little more on keysDown[Key] = true table dictionaries in Lua allow you to make variables with a string value. For example:

table = 
{
    ["hi"] = "hello"
}

print(table.hi)
-- prints hello

This basically is all keysDown[Key] = trueis doing. It would actually make something like this:

table =
{
    ["a"] = true
}
1
Correct! Table.a is just shorthand for Table["a"] adark 5487 — 10y
1
Although, I should mention, it's just Lua, not "LUA". Lua means 'Moon' (as in, *the* Moon) in Porteugeese, it is not an acronym. adark 5487 — 10y
Ad