I'm pretty new to scripting, and I still don't quite have the hang of using tables instead of a ton of if statements. I'm trying to call specific functions based on the value of a variable, and I'm pretty sure that tables are the solution to my problem, but I can't figure it out myself. I attached below essentially what I'm trying to do. Any help is greatly appreciated.
local function onInputBegan(inputObject, gameProcessedEvent) if gameProcessedEvent then return end if inputObject.KeyCode.Name == "F" then if var == 1 then require(game.ReplicatedStorage.module1) elseif var == 2 then require(game.ReplicatedStorage.module2) -- and so on end end end
You could use tables or use the "var" variable, both works, but the reason that your script doesn't work is probably because you never defined the "var" variable, that means that the script thinks "var" is equal to nil (nothing). So you're basically checking
if (nil == 1) then
You can define the variable. Also to avoid repeating the if statement and module requiring, you COULD use a table, but if you want to have a name like "module1", "module2" "module10" etc, you can concatenate strings to find the name of the module to find, concatenation is a way to connect two strings together. It's supposed to be a "..", For example,
local a = "Hello" local b = "Roblox" print(a..b) -- You're basically connecting two strings together, this will print "HelloRoblox" because you connected "Hello" (a) with "Roblox" (b) print(a.." "..b) -- This will print "Hello World" because you connected Hello (a) with a space and connected that with "Roblox"
You could find the module using this
local moduleName = "module"..var -- This will "module1" if var = 1 and "module10" if var = 10
Here is the full script
local var = 1 local function onInputBegan(inputObject, gameProcessedEvent) if gameProcessedEvent then return end if inputObject.KeyCode.Name == "F" then local moduleName = "module"..tostring(var) -- We turn var into a string because it's a number and not a string if (game.ReplicatedStorage:FindFirstChild(moduleName) and game.ReplicatedStorage:FindFirstChild(moduleName):IsA("ModuleScript")) then -- We check if the module exists and it's a modulescript and not a part or script require(game.ReplicatedStorage[moduleName]) -- Using game.ReplicatedStorage[] is the same as game.ReplicatedStorage. end end end
If you wanted to, you could still use an array for this.
An array is basically a table which can hold any values. Arrays have to be indexed using a number. For example
-- Indexes start at 1 local fruits = { "Oranges", -- Index 1 "Apples", -- Index 2 "Mangoes", -- Index 3 } print(fruits[1]) -- This will print "Oranges" because "Oranges" is at index 1 fruits[1] = "Tomato" -- We replace first index (Oranges) with "Tomato" print(fruits[1]) -- This will print "Tomato"
We can also use the "#" symbol to get the amount of keys in the array. For example
local a = {"A", "B"} print(#a) -- This will print 2 because there are two keys ("A" and "B") a[#a + 1] = "C" -- Saying #a + 1 is basically saying the next index after the last index, this will not override any keys, this will create a new index at the end of the table print(#a) -- this will print 3 now
Since your "var" variable is a number, it can be used as an index. This is your script with a table
local var = 1 local modules = { -- These have to be in order, because we are gonna be using index for this game.ReplicatedStorage.module1, game.ReplicatedStorage.module2, game.ReplicatedStorage.module2 } local function onInputBegan(inputObject, gameProcessedEvent) if gameProcessedEvent then return end if inputObject.KeyCode.Name == "F" then local module = modules[var] -- Getting the module using index if (module and module:IsA("ModuleScript")) then -- If module is not equal to nil (doesn't exist) and it's a module script then require(module) end end end