A module script keeps repeating, nonstop and fast. What can I do to slow it down?

I’ve been trying to add team icon functionality to this script, and I do believe I’m rather close to being able to do so. However, right now I’m trying to figure out why this module script keeps repeating itself, bombarding me data, as well as lagging the entire game. I have tried adding wait statements on this module script, but to no avail
image

Below you’ll find the module script that I’m trying to slow down.

--[[
    Programmer: Nyxaris
    Version: 1.0.3
]]

local IconsManager = {}

--// Dependencies
local Modules = script.Parent
local ServiceManager = require(Modules.ServiceManager)
local SettingsManager = require(Modules.SettingsManager)

--// UI Elements
local Players = ServiceManager.Players
local PlayerList = ServiceManager.PlayerList

--// Functions
function IconsManager.IsFriendOfCurrentPlayer(player)
	local currentPlayer = Players.LocalPlayer
	if currentPlayer and currentPlayer ~= player then
		return currentPlayer:IsFriendsWith(player.UserId)
	end
	return false
end

function IconsManager.UpdatePlayerIcon()
	for _, player in pairs(game.Players:GetPlayers()) do
		local playerFrame = PlayerList:FindFirstChild(player.Name .. "_Frame")
		if playerFrame then
			local iconSet = false
			local header = playerFrame.PlayerHeader
			local backgroundColor = header.BackgroundColor3

			if SettingsManager.Settings.groupRanks.enableGroupRanks and SettingsManager.Settings.groupRanks.groupID then
				local rankInGroup = player:GetRankInGroup(SettingsManager.Settings.groupRanks.groupID)
				local rankData = SettingsManager.GroupRanks[rankInGroup]
				if rankInGroup and rankData then
					header.Icon.Image = rankData.icon
					iconSet = true
				end
			end
			
			if player.Team then
				
				local whichTeam =  tostring(player.TeamColor)
				print(whichTeam)
				local teamData = SettingsManager.Team[whichTeam]
					header.Icon.Image = teamData.icon
					iconSet = true
				
			end
			
			if not iconSet then
				local isFriend = IconsManager.IsFriendOfCurrentPlayer(player)
				local EF = 17390341
				local defaultIcon = ""
				local defaultColor = (backgroundColor.R + backgroundColor.G + backgroundColor.B) / 3 > 0.5 and Color3.new(0, 0, 0) or Color3.new(1, 1, 1)

				if isFriend then
					defaultIcon = "rbxassetid://14724599020"
				elseif player:GetRankInGroup(EF) >= 3 then
					defaultIcon = "rbxassetid://15533016053"
				end

				header.Icon.Image = defaultIcon
				header.Icon.ImageColor3 = defaultColor
				iconSet = true
			end

			header.Icon.Visible = iconSet
		end
	end
end

return IconsManager

2 Likes

Maybe add a debounce with wait() or task.wait() (You can put any value inside of the wait functions)

OOPS

I DIDNT READ ONE OF THE SENTENCES YOU WROTE MB

What’s firing the module script?
If it’s getting fired every frame then of course it’s going to lag. I’d look at the other script that calls this one to see if you can limit it to firing the module when a player joins and possibly just update it once a minute if you think having player’s friends updated data necessary. If not just put their friends into a table at the beginning of the game and leave it alone.
Don’t forget to delete the table when they leave.

Aha! I found what’s causing it to fire constantly…
image

Now, how will I go about slowing it down? Would a wait() statement below the updatePlayerList work?

After attempting to make the updatePlayerList run only upon player added, a new problem has occurred. Every single team shows up when it shouldn’t…
image

Here’s the code that caused this:
image

A wait won’t help because the module script should only be firing once each time a player joins. If 3 players join in an hour then it should only fire 3 times.

As far as showing every team, check to see what team a player is on, then only run the code for that team.