What script is causing fps/lag problems?

  1. What do you want to achieve? Trying to make this function not lag the whole server.

  2. What is the issue? Include screenshots / videos if possible! It like drops my frames every time a part of the script calls it. This is very bad as it crash clients.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? Tryed changing some lines of code still has the same problem if I remove the code in the function it works fine so yea.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you! Ok so I just want this to stop lagging the fps/ping and crashing clients. Does anybody know what is causing this?

-- Server
local function GetPlayersOnTeam()
	PlayerInTeams = {}
	TeamsLocked = {}
	Spectators = {}

	for i, plr in pairs(Players:GetPlayers()) do
		if plr.Team.Name == "Spectator" then
			table.insert(Spectators,plr)

			if #TeamsLocked >= 5 then
				for i = 1,2,1 do
					table.remove(TeamsLocked,5)
				end
			end

			if plr.UserId == 1029906486 or plr.UserId == 668285059 or plr.UserId == 648145243 or plr.UserId == 1119697922 then
				table.insert(TeamsLocked,0)
			else
				table.insert(TeamsLocked,1)
			end

			table.insert(TeamsLocked,1)

			if ended == 1 then
				for i = 1,#TeamsLocked,1 do
					table.remove(TeamsLocked,1)
				end

				for i = 1,#PlayerInTeams,1 do
					table.remove(PlayerInTeams,1)
				end

				for i = 1,6,1 do
					table.insert(TeamsLocked,1)
				end

				for i = 1,6,1 do
					table.insert(PlayerInTeams,0)
				end
			end

			UpdateTeams:FireClient(plr,PlayerInTeams,TeamsLocked,Spectators)
		end
	end
-- Client
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local LocalPlayer = Players.LocalPlayer

local Event = ReplicatedStorage:WaitForChild("UpdateTeams")
local Event2 = ReplicatedStorage:WaitForChild("ChooseTeam")

local connection = nil

local Gui = script.Parent

local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local StatusGui = PlayerGui:WaitForChild("StatusGui")
local HealthGui = PlayerGui:WaitForChild("HealthGui")
local InfoGui = PlayerGui:WaitForChild("InfoGui")

local Black = Gui:WaitForChild("Black Team")
local White = Gui:WaitForChild("White Team")
local Green = nil
local Yellow = nil
local Cyan = Gui:WaitForChild("Cyan Team")
local Ran = Gui:WaitForChild("Random Team")

local Error = Gui:WaitForChild("Error")

local textlabel = StatusGui:WaitForChild("LeaderboardFrame"):WaitForChild("Spectator")

local Spectators = {}

local locks = nil
local maxlocks = 5

for i, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("ImageButton") then
		v.Activated:Connect(function()	
			if v:FindFirstChild("Locked") then
				if not v.Locked.Visible then
					Event2:FireServer("Change Team",v.Name)
					StatusGui.Enabled = true
					HealthGui.Enabled = true
					InfoGui.Enabled = true
					Gui.Enabled = false

					Error.Visible = false
				else
					Error.Visible = true

					Error.Visible = true
				    Error.Text = "Error: Unable to assign team! Please try a different team or wait some time."
				end
			else
			    Error.Visible = true
				Error.Text = "Opps! That team is currently unavailable."
			end
		end)
	end
end

local function UpdateTeams(Data1,Data2,Data3,ChosenCamera)
	for i, v in pairs(Data1) do
		if i == 1 then
			Black.Title.Text = v
		elseif i == 2 then
			White.Title.Text = v
		elseif i == 3 then
		    
		elseif i == 4 then
		    
		elseif i == 5 then
			Cyan.Title.Text = v
		end
	end

	for i, v in pairs(Data2) do
		if i == 1 and v == 0 then
		    Black.Visible = true
			Black.Locked.Visible = false
		elseif i == 1 and v == 1 then
			Black.Visible = true
			Black.Locked.Visible = true
		elseif i == 2 and v == 0 then
			White.Visible = true
			White.Locked.Visible = false
		elseif i == 2 and v == 1 then
			White.Visible = true
			White.Locked.Visible = true
		elseif i == 3 and v == 0 then
			
		elseif i == 3 and v == 1 then
			
		elseif i == 4 and v == 0 then
			
		elseif i == 4 and v == 1 then
		
		elseif i == 5 and v == 0 then
			Cyan.Visible = true
			Cyan.Locked.Visible = false
		elseif i == 5 and v == 1 then
			Cyan.Visible = false
			Cyan.Locked.Visible = true
		elseif i == 6 and v == 0 then
			Ran.Visible = true
			Ran.Locked.Visible = false
			Black.Visible = false
			Black.Locked.Visible = true
			White.Visible = false
			White.Locked.Visible = true
			Cyan.Visible = false
			Cyan.Locked.Visible = true
		elseif i == 6 and v == 1 then
			Ran.Visible = false
			Ran.Locked.Visible = true
		end
	end

	Spectators = {}

	textlabel.Text = "Spectators: None"

	for i, v in pairs(Data3) do
		table.insert(Spectators,v.Name)
		textlabel.Text = "Spectators: "..table.concat(Spectators, ", ") --> 1; 2; 3; 4
	end
	
	if connection then
		connection:Disconnect()
		
		wait(0.25)
		
		connection = Event.OnClientEvent:Connect(UpdateTeams)
	end
end

connection = Event.OnClientEvent:Connect(UpdateTeams)

You have many bucle FOR, uhm…
Sorry for my english but i’m gonna try to help you.

First: you can use Connection.Connected to know if some function are activated in the variable

Second: try not to use table.insert, instead use Table [#Table + 1] = Value

The problem may could be Bucle for with Players:GetPlayers()
with the line of FireClient(), place that lineout of the loop to prevent the for loop from firing that function multiple times
Tell me if that works
: D

Connection.Connected: RBXScriptConnection

Edit: And what is this lines? image
Table.Remove(TeamsLocked, 5) is like: Table[5] = nil… i think, read about it here: table

1 Like

Suppose to lock all teams when the round is over. Also thanks for you help I appreciate it.

1 Like

Could you fix the problem of lag?

1 Like

Um yes it works it fixed the problem I also rewrited some parts of my script.

Edit: Is table.sort ok to use?

I don’t find any problem in internet, it’s good i think and good for you