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.
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] = true
is doing.
It would actually make something like this:
table = { ["a"] = true }
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?