ModuleScript problem

So every function that I put this this module script seems to always have “self: the module’s name” first.


and I can’t find a way to not make that happen, and it would be so nice if someone would spot out a solution for me.

Here’s my modulescript if you wanna look through it. I know it looks a lot, but everything works aside from the functions.

local AttackEngine = {}
local MainModule = {
	["GeneralStats"] = {
		-- General Stats
		["Monitor Head"] = {
			["Name"] = "MONITORHEAD",
			["Type"] = "Electric",
			["HP"] = 175,
			["Gender"] = "Male",
			["Stage"] = "1",
			["Icons"] = {
				["PlayerSide"] = {
					["Normal"] = "rbxassetid://12234029454",
					["Low Health"] = "rbxassetid://12234029987"
				},
				["EnemySide"] = {
					["Normal"] = "rbxassetid://12228142775",
					["Low Health"] = "rbxassetid://12220795632"
					}
				}
		},
		["Toast"] = {
			["Name"] = "TOAST",
			["Type"] = "Electric",
			["HP"] = 150,
			["Gender"] = "Male",
			["Stage"] = "1",
			["Icons"] = {
				["PlayerSide"] = {
					["Normal"] = "rbxassetid://12220791434",
					["Low Health"] = "rbxassetid://12229665036"
				},
				["EnemySide"] = {
					["Normal"] = "rbxassetid://12234146928",
					["Low Health"] = "rbxassetid://12234147496"
				}
			}
		},
		["Snortoise"] = {
			["Type"] = "Water",
			["HP"] = 175,
			["Gender"] = "Male",
			["Stage"] = "1"
		}
	},
}

-- Attack Function !!
function MainModule:Attack(name)
	if MainModule[name] == "Monitor Head" then
		local AttackMoveRandom = math.random(1,2)
	if AttackMoveRandom == 1 then
		local Text = "MONITORHEAD used Flash!"
		MainModule.Text(Text)
		-- Flash
		wait(2)
		local Miss = math.random(1,5)
		if Miss == 3 then
			-- Missed
			local Text = "MONITORHEAD missed!"
			MainModule.Text(Text)
		elseif Miss == 1 or 2 then
			-- Hit
			local CriticalChance = math.random(1,5)
			if CriticalChance == 5 then
				-- Critical Hit
				local Text = "Critical Hit!"
				MainModule.Text(Text)
				local DMG = math.random(40,55)
			elseif CriticalChance == 1 or 2 or 3 or 4 then
				-- Normal Hit
				local DMG = math.random(10,25)
				local Text = "TOAST has been hit!"
				MainModule.Text(Text)
			end
		end
	elseif AttackMoveRandom == 2 then
		local Text = "MONITORHEAD used Antenna Strangle!"
			MainModule.Text(Text,Text)
		-- Antenna Strangle
		wait(2)
		local Miss = math.random(1,5)
		if Miss == 3 then
			-- Missed
			local Text = "MONITORHEAD missed!"
				MainModule.Text(Text,Text)
		elseif Miss == 1 or 2 then
			-- Hit
			local CriticalChance = math.random(1,5)
				MainModule.Text(Text,Text)
			if CriticalChance == 5 then
				-- Critical Hit
				local Text = "Critical Hit!"
					MainModule.Text(Text,Text)
				local DMG = math.random(20,30)
			elseif CriticalChance == 1 or 2 or 3 or 4 then
					-- Normal Hit
				local also = workspace.BattleInfo.PlayerSide:WaitForChild("NameValue").Value 
				local DMG = math.random(10,20)
				local Text = also .. " has been hit!"
					MainModule.Text(Text,Text)
			end
		end
		end
	elseif MainModule[name] == "Toast" then
		local Icon = game.StarterGui.ScreenGui.Frame.PlayerIcon
		local EndPos = UDim2.new(-0.007, 0,0.413, 0)
		local AttackPos = UDim2.new(0.082, 0,0.302, 0)
		Icon:TweenPosition(AttackPos,Enum.EasingDirection.InOut,Enum.EasingStyle.Sine,0.45,true)
		wait(0.45)
		Icon:TweenPosition(EndPos,Enum.EasingDirection.InOut,Enum.EasingStyle.Sine,0.45,true)
		local Text = "TOAST used Malted Rye!"
		-- Flash
		MainModule.Text(Text,Text)
		wait(2)
		local Miss = math.random(1,5)
		if Miss == 3 then
			-- Missed
			local Text = "TOAST missed!"
			MainModule.Text(Text,Text)
		elseif Miss == 1 or 2 then
			-- Hit
			local CriticalChance = math.random(1,5)
			if CriticalChance == 5 then
				-- Critical Hit
				local Text = "Critical Hit!"
				game.Workspace.BattleInfo.EnemySide:WaitForChild("HealthValue").Value = game.Workspace.BattleInfo.EnemySide:WaitForChild("HealthValue").Value - math.random(30,45)
				MainModule.Text(Text,Text)
			elseif CriticalChance == 1 or 2 or 3 or 4 then
				-- Normal Hit
				local Text = "MONITORHEAD has been hit!"
				game.Workspace.BattleInfo.EnemySide:WaitForChild("HealthValue").Value = game.Workspace.BattleInfo.EnemySide:WaitForChild("HealthValue").Value - math.random(20,25)
				MainModule.Text(Text,Text)
			end
		end
	elseif MainModule[name] == "DamagedToast" then
		local Icon = game.StarterGui.ScreenGui.Frame.PlayerIcon
		local EndPos = UDim2.new(-0.007, 0,0.413, 0)
		local AttackPos = UDim2.new(0.082, 0,0.302, 0)
		Icon:TweenPosition(AttackPos,Enum.EasingDirection.InOut,Enum.EasingStyle.Sine,0.45,true)
		wait(0.45)
		Icon:TweenPosition(EndPos,Enum.EasingDirection.InOut,Enum.EasingStyle.Sine,0.45,true)
		local Text = "TOAST used Charred Pumpernickel!"
		-- Flash
		MainModule.Text(Text,Text)
		wait(2)
		local Miss = math.random(1,5)
		if Miss == 3 then
			-- Missed
			local Text = "MONITORHEAD missed!"
			MainModule.Text(Text,Text)
		elseif Miss == 1 or 2 then
			-- Hit
			local CriticalChance = math.random(1,5)
			if CriticalChance == 5 then
				-- Critical Hit
				local Text = "Critical Hit!"
				game.Workspace.BattleInfo.EnemySide:WaitForChild("HealthValue").Value = game.Workspace.BattleInfo.EnemySide:WaitForChild("HealthValue").Value - math.random(40,45)
				MainModule.Text(Text,Text)
			elseif CriticalChance == 1 or 2 or 3 or 4 then
				-- Normal Hit
				local Text = "MONITORHEAD has been hit!"
				game.Workspace.BattleInfo.EnemySide:WaitForChild("HealthValue").Value = game.Workspace.BattleInfo.EnemySide:WaitForChild("HealthValue").Value - math.random(20,25)
				MainModule.Text(Text,Text)
			end
		end
	end
end

function MainModule:Text(text)
	for i = 1, #text do
		game.StarterGui.ScreenGui.Frame.TextFrame.TextLabel.Text = string.sub(text, 1, i)
		if string.sub(text, i, i) == "." then
			wait(0.3)
		end
		wait(0.04)
end

function MainModule:GetBattleInfo(Name, Enemy)
	if MainModule[Name] == "Monitor Head" and Enemy then
		local HealthValue = Instance.new("NumberValue")
		local NameValue = Instance.new("StringValue")
		NameValue.Parent = workspace.BattleInfo.EnemySide
		NameValue.Value = MainModule.GeneralStats["Monitor Head"].Name
		HealthValue.Parent = workspace.BattleInfo.EnemySide
		HealthValue.Value = MainModule.GeneralStats["Monitor Head"].HP
	elseif MainModule[Name] == "Monitor Head" and not Enemy then
		local HealthValue = Instance.new("NumberValue")
		local NameValue = Instance.new("StringValue")
		NameValue.Parent = workspace.BattleInfo.Parent
		NameValue.Value = MainModule.GeneralStats["Monitor Head"].Name
		HealthValue.Parent = workspace.BattleInfo.PlayerSide
		HealthValue.Value = MainModule.GeneralStats["Monitor Head"].HP
	elseif MainModule[Name] == "Toast" and Enemy then
		local HealthValue = Instance.new("NumberValue")
		local NameValue = Instance.new("StringValue")
		NameValue.Parent = workspace.BattleInfo.EnemySide
		NameValue.Value = MainModule.GeneralStats.Toast.Name
		HealthValue.Parent = workspace.BattleInfo.EnemySide
		HealthValue.Value = MainModule.GeneralStats.Toast.HP
	elseif MainModule[Name] == "Toast" and not Enemy then
		local HealthValue = Instance.new("NumberValue")
		local NameValue = Instance.new("StringValue")
		NameValue.Parent = workspace.BattleInfo.PlayerSide
		NameValue.Value = MainModule.GeneralStats.Toast.Name
		HealthValue.Parent = workspace.BattleInfo.PlayerSide
			HealthValue.Value = MainModule.GeneralStats.Toast.HP
		end
	end
end

function MainModule:DeleteBattleInfo()
	local PlrValues = workspace.BattleInfo.PlayerSide
	local EnemyValues = workspace.BattleInfo.EnemySide
	PlrValues:ClearAllChildren()
	EnemyValues:ClearAllChildren()
end


return MainModule

The module function uses : and not . maybe thats the problem?

2 Likes

You are using a different operator

Lets say you have a function known as: module:Thing()

But when you use it, you put: module.Thing() this is valid, and may not look like much, but the script will now require a self parameter for OOP (Object Oriented Programming) stuff, to fix it, you would write how it is normally, which is: module:Thing()

A Colon (aka the : symbol) function is different from a Period (aka the . symbol) function, one of them has self already specified while the other needs it to be specified.

I would Recommend looking into OOP (Object Oriented Programming) for more details about it, it may also help with your code as it currently looks a bit messy.

Its a bit of a complex topic.

2 Likes

Just use a colon instead of a period

Explanation

When you use : instead of ., the MainModule is already passed to the function, known as self. However, the . operator needs self to be passed in manually.

Simply put, using a period declares a function as a function, and using a colon declares it as a method. In a method, self is always the object being declared on.

mob:Attack()

This method calls for one parameter, self, which is whatever we’re calling this method with, in this case, mob.
However:

mob.Attack()

This only assumes you’re calling a function called Attack within mob, which is not a class.

A method always has one parameter, self, passed to the function as the first parameter, as the class we called the method upon. A function is, well, a function.

1 Like

Oh my god I feel stupid lol, but thank you guys for helping me notice this problem.

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