Support on Module Scripts

I am using a module script for the prices and names of certain weapons that a player can buy through an NPC and in the module script there are tables for the 3 different weapons, so how am I meant to find the weapon name in the tables, I do have the names of the weapons in a folder in server storage.
I don’t want to separately write if the name of the choice was “M4A1” then refer to that table, I want it to be shorter and more compact in the script so that it’s not done manually.

Do something like this instead.

local module = {}

module["AK-47"] = {
     Cost = 4500;
     -- other stuff
}

module["M4A1"] = {
     Cost = 4500;
     -- other stuff
}

-- etc

return module

But what if I didn’t want it to be like that, in that certain layout.

What’s wrong with that layout?

It’s faster than a for-loop going through every index, and easier to read.

I’m not sure, I just don’t prefer that layout, and I would rather have a for loop going through every index and finding the certain weapon name after the client script has given the server the name of the choice the player has selected.

I really think you should consider using the method that I provided. It’s a lot faster than whatever you have in mind. Utilize tables wisely, they’re the most powerful tool in Lua.

I see what you mean, I dont know if you can have a different layout with what you’re trying to do.

I understand but I just prefer the layout that I used because it just suits to my style and I would rather stick with it instead of using another layout, which is the one you provided. Additionally, if you’re going to tell me to do it manually like this, then I’m certainly not going to be doing that, because it’s just not effective and it just looks terrible.
image

I would be storing a list of weapons as an array and a key because both can be used to get the data that is needed.

Your NPC should not know what weapons are for sale (if they do then they should not have hard coded weapon names). It only need to ask the module what weapons there are. Which is when you need to code additional functions in to get the data that is needed.

When creating a module to hold settings I would use a function. This is so you use the same format for all of your data being added to the table.

sample

local weaponList = {}
local weaponListCount = 0

local module = {}

local function addWeapon(name, cost) -- ect .....
	if weaponList[name] then error("Duplicate name found " .. name) end
	
	-- shared table
	local tmp = {
		Name = name,
		Cost = cost	
	}
	
	-- map name to data
	weaponListCount = weaponListCount + 1
	weaponList[name] = tmp
	weaponList[weaponListCount] = tmp
end

-- list of weapons
addWeapon("AK-47", 4500)
addWeapon("M4A1", 4500)


-- number of available weapons
function module.getWeaponCount()
	return weaponListCount
end

-- check if the weapon exists
function module.hasWeapon(name)
	return weaponList[name] ~= nil
end

-- get the weapon data by name. this should return a copy of the data!!
function module.getWeapon(name)
	if module.hasWeapon(name) then
		return weaponList[name] -- this should be a copy of the data!!
	end
end

-- find the first weapon the player can buy
function module.canBuy(cash)
	for i=1, weaponListCount do
		if weaponList[i].Cost <= cash then
			return weaponList[i].Name
		end
	end
end

-- get a list of weapon names
function module.getWeaponNames()
	local tmp = {}
	for i=1, weaponListCount do
		tmp[i] = weaponList[i].Name
	end
end

return module

There are a lot of ways you can build a module to act as a weapons list so you need to define how you want to access the data. You should not be hard coding the weapons names into the NPC there are a lot of better solutions available.

3 Likes

Alright, I’ll try this solution out and see if it benefits my layout, thank you.

Thinging about my code a bit more. It would be better to have the weapon data in two lists not just all in weaponList.

Because you would just hide this implementation detail behind the functions in the module. I would think “how the module stored data is up to it” it just need to provide the functions for accessing the data.

Personally I’d probably go with an object oriented approach.

It depends on how the data is being used. OOP is nice but not an essential part. If it fits use it and if it does not then there is no real loss.