Module keep returning old result

Now, this can all just be my mistake but i couldnt find any.

Script:

while true do
	wait(0.2)
	local e = require(script.FindTarget)
	print(e)
end

Module:

local Tower = script.Parent.Parent
local Enemies = game.Workspace.TowerDefense.Enemies

local Info = require(Tower.TowerInfo)
local Range = Info.Range

local PossibleTarget = {}

for i,Enemy in pairs(Enemies:GetChildren()) do
	local EP = Vector3.new(0,0,0)
	
	if Enemy:FindFirstChild("Center") ~= nil then
		EP = Enemy:FindFirstChild("Center").Position
	else
		EP = Enemy.Torso.Position
	end
	
	EP2 = Vector3.new(EP.X,Tower.TowerCenter.Position.Y,EP.Z)
	
	local D = (Tower.TowerCenter.Position- EP2).magnitude
	
	if D <= Range/2 then
		
		if Enemy.EnemyStats.IsHidden.Value == false then
			table.insert(PossibleTarget,Enemy)
			
		elseif Info.SeeHidden == true then
			table.insert(PossibleTarget,Enemy)
			
		end
		
	end
end

local ComparingTable = {}

for i,E in pairs(PossibleTarget) do
	local CP = E.CP.Value -1
	local TotalWalked = 0
	
	repeat
		local CPO = game.Workspace.TowerDefense.Paths:WaitForChild("P"..CP)
		
		local EP = Vector3.new(0,0,0)

		if E:FindFirstChild("Center") ~= nil then
			EP = E:FindFirstChild("Center").Position
		else
			EP = E.Torso.Position
		end
		
		local D = (Vector3.new(CPO.Position.X,EP.Y,CPO.Position.Z) - EP).magnitude
		TotalWalked = TotalWalked +D
		CP = CP -1
		
	until CP == 0
	local NewTable = {EN = E,TW = TotalWalked}
	table.insert(ComparingTable,NewTable)

end


local m = -math.huge
local SelectedTarget = nil

for i,CT in pairs(ComparingTable) do
	local TotalWalked = CT.TW
	SelectedTarget = CT.EN
	m = math.max(TotalWalked,m)
	
end

--print(SelectedTarget)
return SelectedTarget

For whatever reason it keep printing “Dummy2” even tho i tried deleting the dummy and it still printing the same, so i think that the module only run once and keep using that same result.

I even tried disabling and enabling it, it still print “Dummy2”! For whatever reason the module still keep returning the first result!

I tried changed it to returning a function and it worked, but i still wanted to know why it didnt work in the first place. This is just me wanting to know now.

Any code within a module script will be only executed the first time it’s been required

-- some module script
print("something test")
local module = {}
module.number = 10
return module
-- some other script
require(module) -- print's "something test"
require(module) -- (nothing)

In your code provided, all that code to find the selected target be executed once, and that’s it. (since it’s just code in a module)
Returning a function works, because that function can be used to execute code.

2 Likes

The returned variable “SelectedTarget” will always point to the same value regardless of how many times you require the module (in different scripts).

1 Like

I believe this is because of the way require caches an instance of a module and re-uses it on subsequent uses of require on the same module.

Modules aren’t really intended to be used in this way. You are effectively using the module as if it is a function, which is fine if you want it to always return the same result, such as a config object but won’t work if you expect it to return a different result each time.

If you look at the best practice for scripting it is recommended that all require statements are put towards the top of the script after references to services are obtained. This suggests their intended use is to return objects or values that can be used later. The most common use of modules is to return an object that provides utility functions or fieds (AKA methods and properties).

In your case since the module is a child of the script, why not just move it to a local function within the script and call it from there.

If it is something that can be used elsewhere then have the module return an object (table) with a function that can be called by the script.

1 Like