Why does the server lag everytime I press touchdown

Hello Developers,
So I am a developer for a league with 300 memebers and lately they been complaining about everytime I press the TD or the add or remove a point button to add the points it just lags the whole server and I’m not sure how to fix it Please help.

TD Reciver:

local player = game:GetService("Players").LocalPlayer
local remote = game:GetService("ReplicatedStorage"):WaitForChild("Scoreboard")
local frame = script.Parent
local label = player:WaitForChild("PlayerGui"):WaitForChild("Messages").Frame.NotificationMsg

function slide(team)
	wait()
	frame.BackgroundColor3 = team.BackgroundColor3
	frame.Top.BackgroundColor3 = team.Top.BackgroundColor3
	frame.Middle.Score.Digit.Text = team.Middle.Score.Digit.Text
	frame.Middle["Team Logo"].Image = team.Middle["Team Logo"].Image
	frame.Top.TeamName.Text = team.Top.TeamName.Text
	frame.Top.TeamRank.Text = team.Top.TeamRank.Text
	wait()
	frame:TweenSize(UDim2.new(.975,0,0.73,0), 'Out', 'Quint', 0.5)
	frame.TDText:TweenSizeAndPosition(UDim2.new(0.65,0,0.7,0), UDim2.new(0.349,0,0.172,0), 'Out', 'Quint', 4)
	wait(5)
	frame:TweenSize(UDim2.new(0,0,0.73,0), 'Out', 'Quint', 0.5)
	frame.TDText.Size = UDim2.new(0.45,0,0.5,0)
	frame.TDText.Position = UDim2.new(0.42,0,0.26,0)
end

function flag()
	local flagBox = frame.Parent.CTRL.Down.Flag
	flagBox:TweenSize(UDim2.new(1,0,1,0), 'Out', 'Quint', 0.5)
	wait(5)
	flagBox:TweenSize(UDim2.new(0,0,1,0), 'Out', 'Quint', 0.5)
end

remote.OnClientEvent:Connect(function(arg, arg2, arg3)
	if arg == "TD H" then
		slide(frame.Parent.Home)
	elseif arg == "TD A" then
		slide(frame.Parent.Away)
	elseif arg == "message" then
		if arg2 == "Subs Done" then
			if label.Text == "Subs" then
				label.Text = ""
			end
		else
			label.Text = arg2
			wait(5)
			if label.Text == arg2 then
				if label.Text ~= "Subs" then
					label.Text = ""
				end
			end
		end
	elseif arg == "Flag" then
		flag()
	elseif arg == "Kickoff" then
		frame.Parent.CTRL.Down.DownText.Text = "Kickoff"
	elseif arg == "spawns" then
		if player.Team == arg2 then
			player.TeamColor = arg3
		end
	end
end)

Ref Operator:

local folder = workspace:FindFirstChild("Scoreboard")
local remote = game:GetService("ReplicatedStorage"):WaitForChild("Scoreboard")
local chains = workspace.Chains
local yard = 3.65
local sounds = folder.Sounds
local spawns = workspace:FindFirstChild("Spawns")
local Players = game:GetService("Players")

--vals
local clock = folder.Time
local pc = folder.PC
local home = folder.Home
local away = folder.Away
local hScore = home.Score
local aScore = away.Score
--

function setMessage(text)
	remote:FireAllClients("message", text)
end

function getTDYards()
	if chains.Flipped.Value then
		return (math.abs(math.floor(((chains.LOS.Line.Position.Z - chains.Yardlines.GoalTwo.Position.Z)/yard) + .5)))
	else
		return (math.abs(math.floor(((chains.LOS.Line.Position.Z - chains.Yardlines.GoalOne.Position.Z)/yard) + .5)))
	end
end

function stopClock()
	clock.Script.Disabled = true
end

function playClock()
	clock.Script.Disabled = false
end

function whistle()
	sounds.Whistle:Play()
end

function stopSound(snd)
	local a1 = snd.Volume
	for i=1, 9 do
		wait()
		snd.Volume = snd.Volume - (a1/10)
	end
	snd.Volume = 0
	snd:Stop()
	snd.Volume = a1
end

function stopCheer()
	wait(75)
	if sounds.Cheer.Playing then
		stopSound(sounds.Cheer)
	end
end

function changeSpawns(spawnn, name, color)
	local team = nil
	local teams = game:GetService("Teams")
	for _, v in pairs(teams:GetChildren()) do
		if v.TeamColor == spawnn.TeamColor then
			team = v
		end
	end
	if team then
		remote:FireAllClients("spawns", team, color)
		spawnn.TeamColor = color
		team.TeamColor = color
		team.Name = name
	end
end

function stopSounds(except)
	for _, v in pairs(sounds:GetChildren()) do
		if v.Playing then
			if v.Name ~= "Cheer" and v.Name ~= "Crowd1" and v.Name ~= "Whistle" and v ~= except then
				stopSound(v)
			end
		end
	end
end

remote.onServerEvent:Connect(function(player, arg, teamname, rank, record, arg2, arg3)
	if arg == "+1M" then
		clock.Value = clock.Value + 60
	elseif arg == "+1S" then
		clock.Value = clock.Value + 1
	elseif arg == "+30S" then
		clock.Value = clock.Value + 30
	elseif arg == "-1M" then
		clock.Value = clock.Value - 60
	elseif arg == "-1S" then
		clock.Value = clock.Value - 1
	elseif arg == "-30S" then
		clock.Value = clock.Value - 30
	
	elseif arg == "1Away" then
		aScore.Value = aScore.Value + 1
	elseif arg == "1Home" then
		hScore.Value = hScore.Value + 1
	elseif arg == "-1Away" then
		aScore.Value = aScore.Value - 1
	elseif arg == "-1Home" then
		hScore.Value = hScore.Value - 1
	elseif arg == "FGAway" then
		whistle()
		aScore.Value = aScore.Value + 3
		setMessage(away.Team.Value .. " field goal is good")
	elseif arg == "FGHome" then
		whistle()
		hScore.Value = hScore.Value + 3
		setMessage(home.Team.Value .. " field goal is good")
	elseif arg == "TDAway" then
		whistle()
		stopClock()
		aScore.Value = aScore.Value + 6
		setMessage(getTDYards() .. " yard " .. away.Team.Value .. " Touchdown!")
		remote:FireAllClients("TD A") -- sends to client to slide
		local sound = sounds:FindFirstChild(away.Team.Value)
		if sound then
			sound:Play()
			stopSounds(sound)
		end
	elseif arg == "TDHome" then
		whistle()
		stopClock()
		sounds.Cheer:Play()
		spawn(stopCheer)
		hScore.Value = hScore.Value + 6
		setMessage(getTDYards() .. " yard " .. home.Team.Value .. " Touchdown!")
		remote:FireAllClients("TD H") -- sends to client to slide
		local sound = sounds:FindFirstChild(home.Team.Value)
		if sound then
			sound:Play()
			stopSounds(sound)
		end
		
	elseif arg == "PlayGC" then
		playClock()
	elseif arg == "StopGC" then
		stopClock()
	elseif arg == "ResetGC" then
		clock.Value = clock.MaxValue
	
	elseif arg == "PlayPC" then
		if pc.Value == 0 then pc.Value = pc.MaxValue end
		pc.Script.Disabled = false
	elseif arg == "StopPC" then
		pc.Script.Disabled = true
	elseif arg == "ResetPC" then
		pc.Value = pc.MaxValue
		
	elseif arg == "TimeoutAAdd" then
		away.TOS.Value = away.TOS.Value + 1
	elseif arg == "TimeoutHAdd" then
		home.TOS.Value = home.TOS.Value + 1
	elseif arg == "TimeoutASub" then
		whistle()
		away.TOS.Value = away.TOS.Value - 1
		setMessage("Timeout " .. away.Name)
	elseif arg == "TimeoutHSub" then
		whistle()
		home.TOS.Value = home.TOS.Value - 1
		setMessage("Timeout " .. home.Name)
	
	elseif arg == "Q1" then
		folder.Quarter.Value = 1
	elseif arg == "Q2" then
		folder.Quarter.Value = 2
	elseif arg == "Q3" then
		folder.Quarter.Value = 3
	elseif arg == "Q4" then
		folder.Quarter.Value = 4
	elseif arg == "OT" then
		if folder.Quarter.Value >= 4 then
			folder.Quarter.Value = folder.Quarter.Value + 1
		else
			folder.Quarter.Value = 5
		end
	
	elseif arg == "aChange" then
		away.Team.Value = teamname
		away.Record.Value = record
		if rank == "" then
			away.Rank.Value = 0
		else
			away.Rank.Value = rank
		end
	elseif arg == "hChange" then
		home.Team.Value = teamname
		home.Record.Value = record	
		if rank == "" then
			home.Rank.Value = 0
		else
			home.Rank.Value = rank
		end
	
	elseif arg == "HomeSpawns" then
		changeSpawns(spawns.Home, arg2, arg3)
		
	elseif arg == "AwaySpawns" then
		changeSpawns(spawns.Away, arg2, arg3)
	
	elseif arg == "Flag" then
		whistle()
		setMessage("Flag")
		remote:FireAllClients("Flag") -- sends to client to slide
	elseif arg == "FreePlay" then
		whistle()
		setMessage("Flag, FP")
		remote:FireAllClients("Flag") -- sends to client to slide
		
	elseif arg == "Sounds" then
		local song = sounds:FindFirstChild(arg2)
		if song then
			if song.Playing then
				stopSound(song)
			else
				song:Play()
				if song.Name ~= "Cheer" then
					stopSounds(song)
				end
			end
		end
	
	elseif arg == "Flip" then
		folder.HomePos.Value = not folder.HomePos.Value
	
	--calls
	elseif arg == "Call" then
		if arg2 == "AwayTI" then
			whistle()
			setMessage("Timein " .. away.Name)
		elseif arg2 == "HomeTI" then
			whistle()
			setMessage("Timein " .. home.Name)
		elseif arg2 == "Complete" then
			setMessage("Complete")
		elseif arg2 == "Final" then
			whistle()
			setMessage("Final")
		elseif arg2 == "Good" then
			whistle()
			setMessage("Good")
		elseif arg2 == "Incomplete" then
			whistle()
			setMessage("Incomplete")
			stopClock()
		elseif arg2 == "Interception" then
			setMessage("Interception")
		elseif arg2 == "Kickoff" then
			whistle()
			setMessage("Kickoff")
			remote:FireAllClients("Kickoff")
		elseif arg2 == "NoGood" then
			whistle()
			setMessage("No Good")
		elseif arg2 == "OS" then
			whistle()
			setMessage("Onside")
		elseif arg2 == "OTI" then
			whistle()
			setMessage("Officials Timein")
		elseif arg2 == "OTO" then
			whistle()
			setMessage("Officials Timeout")
		elseif arg2 == "PAT" then
			whistle()
			setMessage("PAT")
		elseif arg2 == "TOV" then
			whistle()
			setMessage("Turnover")
		elseif arg2 == "Sack" then
			whistle()
			setMessage("Sack")
		elseif arg2 == "Safety" then
			whistle()
			setMessage("Safety")
		elseif arg2 == "Down" then
			whistle()
			setMessage("Down")
		elseif arg2 == "Subs" then
			whistle()
			setMessage("Subs")
		elseif arg2 == "Subs Done" then
			setMessage("Subs Done")
		end
	
	end -- ends arg if statement
end)
3 Likes

How many if statements are in your script

2 Likes

Oh my goodness, the if statements is whats causing the lag. You have so many of them! Use tables. Put all the args and the messages and stuff in a dictionary, like this:

local args = {
   ["Arg1"] = 123,
   ["Arg2"] = 1234,
   ["Arg3"] = 12345,
   -- etc
}

Then loop through it and work from there.