How to have 1 module handle multiple different parts

I have a module (a gamemode) which loops through a timer looking for specific control points. However my problem is that since I have numerous control points, they all end up sharing the same variables.

So below basically finds the closest point to a player. And then I have code latter down which handles the capturing of said point. However, if I have 2 players capturing 2 seperate points it doesn’t work as intended. How can I get this module to function seperately for each individual control point, instead of looping through everyone

while true do
	local Capturer

	local Capturers = 0
	local Defenders = 0
		
	local ClosestPoint

	for _, player in pairs(Players:GetPlayers()) do
		local Character = player.Character
		if Character then
			local HumanoidRootPart = Character.PrimaryPart
			if HumanoidRootPart then
				local Humanoid = Character:FindFirstChildWhichIsA('Humanoid')
				
				for _, v in pairs(ControlPoints:GetChildren()) do
					if player:DistanceFromCharacter(v.PrimaryPart.Position) <= Range then
						if Humanoid.Health > 0 then
							ClosestPoint = v
						end
					end
				end
            end
        end
    end
    wait()
end

So is it a module script?
30 chars

One of the options should be creating a table for each control point and then set the variables inside the table. This eliminates the use of multiple modules.

If I were to write this from scratch, I’d set functions for each control point or something similar. The function’s scope can use local variables and start a loop through spawn or coroutine.

1 Like