How can I modify my enemy cycle without having to change it inside of its module script

Hello developers.

I’ve created a module script which houses information about a stage (Such as the rewards for example). And inside of said module script is this table:

StageModule.EnemyCycle = {
	"Dummy", 2, 1
}

This is the Enemy Cycle as you can see it’s nearly empty. Basically, what I want is that I wanna modify the Enemy Cycle without having to change it inside of the module script. The things I wanna modify is:

  1. The enemy (The string)
  2. The amount of times the enemy will spawn before switching to the next (First number)
  3. The cooldown between spawning the current enemy (Second number)

You can change the variable of a ModuleScript from another script just like how you would from within the ModuleScript. For example:

Module Script

--Placed in ReplicatedStorage
local StageModule = {}

StageModule.EnemyCycle = {
	"Dummy", 2, 1
}

return StageModule

Script that accesses the ModuleScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local stageModule = require(ReplicatedStorage.StageModule)

--You can modify the value just by indexing the module script
--like how you would inside of the module script itself
stageModule.EnemyCycle = {
	"Dummy2", 4, 2
}

Okay! Oh and also, how can I add more enemies within the table/enemy cycle without having to add some in the module script? The table inside of the module looks similar to this now since I modified it while waiting for a response.

StageModule.EnemyCycle = {
	[1] = {
		["EnemyName"] = "Dummy",
		["Amount"] = 2,
		["Cooldown between Spawns"] = 1
	}
}

Since StageModule.EnemyCycle is a dictionary, you can use simply index the key where you want to add a new value.

Here is how you could implement it:

Solution 1: Dictionary Version

Script accessing the ModuleScript

local StageModule = require(workspace:WaitForChild("StageModule")) --Change this to where the ModuleScript actually is

local newEnemy = {
	["EnemyName"] = "Dummy2",
	["Amount"] = 3,
	["Cooldown between Spawns"] = 1.5
}

--Adding a new enemy to StageModule.EnemyCycle
StageModule.EnemyCycle[2] = newEnemy

I would recommend making StageModule.EnemyCycle a table instead of a dictionary. From what I can see, you can achieve the same result with StageModule.EnemyCycle as a table instead of a dictionary, while making it much easier (in my opinion) to add a new element at the end of a table than at the end of a dictionary.

Explaination

Dictionary Version (Current Version)

StageModule.EnemyCycle = {
	[1] = {
		["EnemyName"] = "Dummy",
		["Amount"] = 2,
		["Cooldown between Spawns"] = 1
	}
}

Table Version

StageModule.EnemyCycle = {
	{
		["EnemyName"] = "Dummy",
		["Amount"] = 2,
		["Cooldown between Spawns"] = 1
	}
}

If you plan to change EnemyCycle to a table instead of a dictionary, you can use table.insert(table, value) instead, which inserts value at the end of table. You can implement it like this:

Solution 2: Table Version

ModuleScript

local StageModule = {}

StageModule.EnemyCycle = {
	{
		["EnemyName"] = "Dummy",
		["Amount"] = 2,
		["Cooldown between Spawns"] = 1
	}
}

return StageModule

Script accessing the ModuleScript

local StageModule = require(workspace:WaitForChild("StageModule")) --Change to where the ModuleScript is actually stored

local newEnemy = {
	["EnemyName"] = "Dummy2",
	["Amount"] = 3,
	["Cooldown between Spawns"] = 1.5
}

--Adding the new enemy
table.insert(StageModule.EnemyCycle, newEnemy)

Alright then, thanks! And also, sorry if this kinda annoys you, but how can I list all of the enemies in the cycle as whenever the player selects a level, I want to add in text which simply says all of the enemies in the level.

For example:
If a player clicks on the first level, the first level will tell you that the only enemy is a Dummy.

Well, that question has less of a concrete answer since I don’t know how you have your game set up. However, I can give a general answer. You can loop through all of the elements within the StageModule.EnemyCycle table (each element will be a dictionary containing the enemy info), and display them by using those values.

Here is an example of what you could do to access the values (assuming you have StageModule.EnemyCycle as a table):

Script
local StageModule = require(workspace:WaitForChild("StageModule")) --You already know how you should change this

--Loop through each enemy in the EnemyCycle and print their info
for index, Enemy in ipairs(StageModule.EnemyCycle) do
	print(index, Enemy["EnemyName"]) --Print the name of the enemy and its position in the table
	print("Amount:", Enemy["Amount"])
	print("Cooldown:", Enemy["Cooldown between Spawns"])
end

Now all you need to do is actually do something with the info instead of just printing it like how currently have it set up. For example, you can instance a new TextLabel for each enemy in the EnemyCycle displaying its name, amount, cooldown, etc.

Alright, thank you very much!

{filllllllller}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.