This modules script is always getting a loading error

I want to make my modules script work. (so it doesn’t get an error while loading)

I always get this error :frowning: image

I looked for this on google error but I don’t find why.

repstorage = game:GetService("ReplicatedStorage")
tweenservice = game:GetService("TweenService")
repfirst = game.ReplicatedFirst
serverstorage= game.ServerStorage

local UNITModule = require(script.Parent.UnitModule)
UnitList = UNITModule.Units()
do
	objects = repfirst:WaitForChild("Objects")
	scripts = serverstorage.Scripts
end
do
	remotes = repstorage.Remotes
	setUnits = remotes.setUnits
end

local gameUnit 

local BUILDING = {}
BUILDING.__index = BUILDING


function BUILDING.SetGame(gameUNIT)
	gameUnit = gameUNIT
end

function BUILDING.New(unit)
	 local newUnit = {}
    setmetatable(newUnit, BUILDING)
    
	newUnit.Class = "Building"
    newUnit.Model = unit.Model:Clone()
	newUnit.Name = unit.Name
	newUnit.MaxHealth = unit.MaxHealth
	newUnit.Health = unit.MaxHealth
	newUnit.Damage = unit.Damage
	newUnit.Cooldown = unit.Cooldown
	newUnit.AttackTick = tick()
	newUnit.Range = unit.Range
	newUnit.BuildRange = unit.BuildRange
	newUnit.BuildTime = unit.BuildTime
	newUnit.CanGarrison = unit.CanGarrison
	newUnit.CanProduce = unit.CanProduce
	newUnit.CPM = unit.CPM
	newUnit.Queue = {}
	newUnit.Color = nil
	newUnit.Active = false
	newUnit.Target = nil
	newUnit.TeamIndex = nil
	newUnit.Script = unit.Script
	
    return newUnit
end

function BUILDING:Spawn(pos)
	local pos = CFrame.new(pos)
	local team = gameUnit:getTeamUnit(self.TeamIndex)
	local playerUnit = team.PlayerUnit
	--local buildings = BUILDING.Buildings()
	--local info = buildings[self.Name]
	self.Model:SetPrimaryPartCFrame(pos)
	self.Model.Parent = workspace
	self.Active = true
	if self.CPM then
		team.CPM = team.CPM + self.CPM
	end
	if playerUnit then
		playerUnit:SetStats(team)
	end
	if self.Script then
		local scriptClone = self.Script:Clone()
		scriptClone.Parent = self.Model
	end
	setUnits:FireAllClients(gameUnit:getBuildings(), false)
end

function BUILDING:Construct(pos, buildTime) --This function YIELDS
	local tickOne = tick()
	local pos = CFrame.new(pos)
	local team = gameUnit:getTeamUnit(self.TeamIndex)
	local playerUnit = team.PlayerUnit
	local children = self.Model:GetChildren()
	local buildingGui = self.Model:FindFirstChild("BuildingGui")
	local buildBar = buildingGui:FindFirstChild("BuildLight", true)
	self.Model:SetPrimaryPartCFrame(pos)
	self.Model.Parent = workspace
	local gameBuildings = gameUnit:getBuildings()
	if playerUnit then
		playerUnit:SetStats(team)
	end
	setUnits:FireAllClients(gameBuildings, false)
	local validChildren = {}
	local cframes = {}
	
	for i = 1, #children do
		if children[i] and children[i]:IsA("Part") and children[i].Name ~= "Base" then
			table.insert(validChildren, children[i])
			table.insert(cframes, children[i].CFrame)
			local startCFrame = CFrame.new(Vector3.new(children[i].Position.X, children[i].Position.Y - 3, children[i].Position.Z))
			children[i].CFrame = startCFrame
			children[i].Transparency = 1
		end
	end
	for i,v in pairs(validChildren) do
		if v and v:IsA("Part") and v.Name ~= "Base" then
			local originalCFrame = cframes[i]
			local tweenInfo = TweenInfo.new(buildTime/#validChildren, Enum.EasingStyle.Linear)
			local propertyTable =
			{
			CFrame = originalCFrame
			}
			local tween = tweenservice:Create(v, tweenInfo, propertyTable)
			tween:Play()
			local startTick = tick()
			while tick() - startTick < buildTime/#validChildren do
				local halfY = v.Size.Y/2
				local posY = v.Position.Y
				local base = self.Model:FindFirstChild("Base")
				if base then
					if posY + halfY > base.Position.Y then
						v.Transparency = 0
					end
				end
				if buildBar then
					local currentTime = tick() - tickOne
					buildBar.Size = UDim2.fromScale(currentTime/buildTime,1)
				end
				wait()
			end
		end
	end
	while tick() - tickOne < buildTime do
		wait()
	end
	buildBar.Parent.Visible = false
end

function BUILDING:SetTeam(team)
	local playerUnit = team.PlayerUnit
	table.insert(team.Buildings, self)
	self.TeamIndex = team.Index
	local children = self.Model:GetChildren()
	self.Color = team.Color
	for i = 1, #children do
		local part = children[i]
		if part.Name == "Recolor" then
			part.Color = team.Color
		elseif self.Name == "Headquarters" and part.Name == "Torso" then
			part.Color = team.Color
		end
	end
end

function BUILDING:GiveGui()
	local buildinggui, garrisongui = objects:FindFirstChild("BuildingGui"), objects:FindFirstChild("GarrisonGui")
	if buildinggui and garrisongui then
		local buildingguiClone, garrisonguiClone = buildinggui:Clone(), garrisongui:Clone()
		buildingguiClone.Parent, garrisonguiClone.Parent = self.Model, self.Model
		buildingguiClone.Adornee, garrisonguiClone.Adornee = self.Model, self.Model
	end
end
function BUILDING:AddQueue(index)
	local unitTable = self.CanProduce[index]
	if unitTable then
		--Insert to Queue
		table.insert(self.Queue, unitTable)
		if #self.Queue - 1 < 1 then
			while #self.Queue > 0 do
				self:Produce(self.Queue[1])
				table.remove(self.Queue,1)
			end
		end
	end
end
function BUILDING:Produce(unitTable) --This function YIELDS
	local buildingGui = self.Model:FindFirstChild("BuildingGui")
	local produceBar = buildingGui:FindFirstChild("ProduceLight", true)
	local newUnit = UNITModule.New(unitTable)
	local team = gameUnit:getTeamUnit(self.TeamIndex)
	local offset = Vector3.new(5,3,5)
	local pos = self.Model.PrimaryPart.CFrame.p + offset
	produceBar.Size = UDim2.fromScale(0,1)
	produceBar.Parent.Visible = true
	while not self.Active do
		wait()
	end
	local startTick = tick()
	while tick() - startTick < unitTable.ProduceTime do
		local currentTime = tick() - startTick
		produceBar.Size = UDim2.fromScale(currentTime/unitTable.ProduceTime,1)
		wait()
	end
	produceBar.Parent.Visible = false
	newUnit:SetTeam(team)
	newUnit:GiveGui()
	newUnit:Spawn(pos)
	team.PlayerUnit:SetStats(team)
end
function BUILDING:Attack(enemyUnit)
	local primaryPart = self.Model.Torso
	local enemyPrimaryPart = enemyUnit.Model.Torso
	local distance = (primaryPart.Position - enemyPrimaryPart.Position).magnitude
	
	self.AttackBool = true
	repeat wait()
		while tick() - self.AttackTick < self.Cooldown do
			wait()
		end
		enemyUnit:TakeDamage(self)
		self.AttackTick = tick()
		if enemyUnit.Health <= 0 then
			enemyUnit:Die(self)
			self.AttackBool = false
		end
	until self.AttackBool == false or (primaryPart.Position-enemyPrimaryPart.Position).magnitude > self.Range 
	
end
function BUILDING:findNearestEnemy()
	
	local model = self.Model
	local primaryPart = model:FindFirstChild("Torso")
	local allies = gameUnit:getTeamUnit(self.TeamIndex)
	if allies then
		allies = allies.AlliesIndex
	end
	local enemyTeams = {}
	for i = 1, #gameUnit.Teams do
		local team = gameUnit.Teams[i]
		local ally = false
		for index = 1, #allies do
			local allyIndex = allies[index]
			if allyIndex == team.Index then
				ally = true
			end
		end
		if not (self.TeamIndex == team.Index) and not ally then
			table.insert(enemyTeams, team)
		end
	end
	local nearestUnit, nearestMagnitude = nil, math.huge
	
	for i = 1, #enemyTeams do
		local enemyUnits = enemyTeams[i].Units
		local enemyBuildings = enemyTeams[i].Buildings
		for index = 1, #enemyUnits do
			local enemyUnit = enemyUnits[index]
			local enemyPrimaryPart = enemyUnit.Model:FindFirstChild("Torso")
			if enemyUnit and enemyPrimaryPart then
				local magnitude = (primaryPart.Position-enemyPrimaryPart.Position).magnitude
				if magnitude < nearestMagnitude then
					nearestUnit, nearestMagnitude = enemyUnit, magnitude
				end
			end
		end
	end
	return nearestUnit, nearestMagnitude
end
function BUILDING:Die(enemyUnit)
	self.Model:Destroy()
	local selfTeam = gameUnit.Teams[self.TeamIndex]
	local teamUnits = selfTeam.Buildings
	for i = 1, #teamUnits do
		local unit = teamUnits[i]
		if unit == self then
--			print("Dead")
			table.remove(teamUnits, i)
			self = nil
		end
	end
	setUnits:FireAllClients(gameUnit:getUnits(), true)
end
function BUILDING:TakeDamage(enemyUnit)
	local model = self.Model
	local unitGui = model:FindFirstChild("BuildingGui")
	self.Health = self.Health - enemyUnit.Damage
	if unitGui then
		local healthGreen = unitGui:FindFirstChild("HealthGreen", true)
		healthGreen.Size = UDim2.fromScale(self.Health/self.MaxHealth, 1)
	end
end
function BUILDING.Buildings()
	
	local buildingFolder = repstorage.Buildings
	local buildingTable =
	{
		 Headquarters =
		{
			Name = "Headquarters";
			Image = "rbxasset://textures/ui/GuiImagePlaceholder.png";
			Model = buildingFolder.Headquarters;
			MaxHealth = 400;
			Damage = 15;
			Cooldown = .9;
			Range = 25;
			BuildRange = 18;
			Cost = 400;
			BuildTime = 25;
			CanGarrison = true;
			CanProduce =  false;
			MagnitudeFunc = "default";
			Script = scripts.AttackBuilding;
		};
		 Outpost =
		{
			Name = "Outpost";
			Image = "rbxasset://textures/ui/GuiImagePlaceholder.png";
			Model = buildingFolder.Outpost;
			MaxHealth = 350;
			Damage = 7;
			Cooldown = 1;
			Range = 23;
			BuildRange = 18;
			Cost = 200;
			BuildTime = 15;
			CanGarrison = true;
			CanProduce =  {UnitList.EliteInfantry};
			MagnitudeFunc = "default";
			Script = scripts.AttackBuilding;
		};
		 Turret =
		{
			Name = "Turret";
			Image = "rbxasset://textures/ui/GuiImagePlaceholder.png";
			Model = buildingFolder.Turret;
			MaxHealth = 135;
			Damage = 4;
			Cooldown = 1;
			Range = 20;
			BuildRange = 8;
			Cost = 60;
			BuildTime = 7;
			CanGarrison = false;
			CanProduce =  false;
			MagnitudeFunc = "default";
			Script = scripts.AttackBuilding;
		};
		 Barracks =
		{
			Name = "Barracks";
			Image = "rbxasset://textures/ui/GuiImagePlaceholder.png";
			Model = buildingFolder.Barracks;
			MaxHealth = 200;
			BuildRange = 18;
			Cost = 150;
			BuildTime = 15;
			CanGarrison = false;
			CanProduce =  {UnitList.Infantry; UnitList.ArmoredInfantry};
			MagnitudeFunc = "default";
			Script = scripts.DefaultBuilding;
		};
			TankPlant =
		{
			Name = "Tank Plant";
			Image = "rbxasset://textures/ui/GuiImagePlaceholder.png";
			Model = buildingFolder.TankPlant;
			MaxHealth = 280;
			BuildRange = 18;
			Cost = 235;
			BuildTime = 15;
			CanGarrison = false;
			CanProduce =  {UnitList.Tank; UnitList.ArmoredTank};
			MagnitudeFunc = "default";
			Script = scripts.DefaultBuilding;
		};
		 Bunker =
		{
			Name = "Bunker";
			Image = "rbxasset://textures/ui/GuiImagePlaceholder.png";
			Model = buildingFolder.Bunker;
			MaxHealth = 550;
			BuildRange = 14;
			Cost = 100;
			BuildTime = 10;
			CanGarrison = true;
			CanProduce =  false;
			MagnitudeFunc = "default";
			Script = scripts.DefaultBuilding;
		};
		OilPump =
		{
			Name = "Oil Pump";
			Image = "rbxasset://textures/ui/GuiImagePlaceholder.png";
			Model = buildingFolder.OilPump;
			MaxHealth = 200;
			Range = 0;
			BuildRange = 18;
			Cost = 65;
			BuildTime = 10;
			CPM = 10;
			CanGarrison = false;
			CanProduce =  false;
			MagnitudeFunc = "plant";
			Script = scripts.DefaultBuilding;
		};
		HeavyOilPump =
		{
			Name = "Heavy Oil Pump";
			Image = "rbxasset://textures/ui/GuiImagePlaceholder.png";
			Model = buildingFolder.HeavyOilPump;
			MaxHealth = 200;
			Range = 0;
			BuildRange = 18;
			Cost = 120;
			BuildTime = 10;
			CPM = 15;
			CanGarrison = false;
			CanProduce =  false;
			MagnitudeFunc = "plant";
			Script = scripts.DefaultBuilding;
		};
	}
	return buildingTable
end


return BUILDING

Thanks for reading :slight_smile:

1 Like

Is that the only error? Did you try running the debugger on the module?

1 Like

My bad, I just found my error :slight_smile:

1 Like