local UserInputService = game:GetService("UserInputService")
local addPart = game.ReplicatedStorage["addPart"]
local removePart = game.ReplicatedStorage["removePart"]
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
local keybinds = {
[Enum.KeyCode.R] = addPart,
[Enum.KeyCode.F] = removePart,
}
local func = keybinds[input.KeyCode]
if func then
func:FireServer()
end
end)
I have been trying to understand this code for ages but I simply cannot grasp the concept.
The part I’m confused about is: keybinds[input.KeyCode]
How does the ‘input’ argument know to match ‘Enum.KeyCode.R’ using tables without doing an Equals To, and then simultaneously also know to specify ‘addPart’ at the right time.
Essentially I don’t really know how ‘input’ is taking the correct keycode, and simultaneously correctly adding addPart correctly in the right place.
I understand the code is basically this: if input.KeyCode == Enum.KeyCode.R then addPart:FireServer()
But I don’t understand the tables part in the thread
If anyone could break it down for me It would be greatly appreciated, thank you
That is how tables (specifically dictionaries here) work fundamentally. With dictionaries, when you supply a key (param lets say) in square brackets, it returns the value that the key assigns. EX:
Its the same story in your code.
When you supplied the button the player pressed to the table in square brackets, you are telling the table to retrieve the value assigned to the key you supplied, if any. If the player pressed C, then the table won’t return anything since Enum.Keycode.C is not a valid key, but when the player presses R the table returns addpart since Enum.Keycode.R is a valid key
The keys and the remote events are connected, therefore if you press the R key, the addPart event would fire, and if you press F, the removePart event would fire instead. The values were assigned in the dictionary
That’s how I interpret it anyway, but @MysteryX2076 's explanation is better and more in depth
I think what also confused me with this table is that when using tables like this, the value is technically an key/item (reversed basically)
local testing = {
["keycode R"] = "addPart"
}
for k,v in testing do
valueAsKey = testing[k]
print(valueAsKey) -- addpart
print(testing[k]) -- addPart
print(testing[v]) -- nil
print (k) -- keycode R
print (v) -- addPart
end
anyway thank you, I kind of understand the grasp of it now. guess I need to keep in mind that values can be technically items/keys if you try to fetch them in hard-brackets like that
InputBegan returns an inputobject. We can check the input’s keycode. You have a dictionary, where 2 specific keycodes have been given a value.
So we check, ‘did we make an entry with the current input’s keycode in the keybinds dictionary?*’. If there is one, then that entry is returned (which is addPart or removePart), and later fired with func:FireServer()
Here’s a different way of doing it, which may be easier to understand:
local UserInputService = game:GetService("UserInputService")
local addPart = game.ReplicatedStorage["addPart"]
local removePart = game.ReplicatedStorage["removePart"]
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
local func
if input.KeyCode == Enum.KeyCode.R then
func = addPart
end
if input.KeyCode == Enum.KeyCode.F then
func = removePart
end
if func then
func:FireServer()
end
end)