Better Optimize Script

Hey Devs. When I was scripting my arm following script. I ran into a problem which whole semi-freeze the game or would heavily lag the game. And I’ve been trying to figure out how I can Optimize it better so it looks like there’s little to no lag at all. But I’m clueless on how I’d do it.

– Server

-- Services --
local PlrService = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
-- Constants / Folder --
local FloorModel = script.Parent:FindFirstChild('Floor')
local CameraFolder = script.Parent:FindFirstChild('Cameras')
local PreventionFolder = script.Parent:FindFirstChild('Prevention')
local QueuingFolder = script.Parent:WaitForChild('Queuing')
-- Constants / Remote --
local GameRemotes = ReplicatedStorage:FindFirstChild('GameRems')
local QueueEvent = GameRemotes:FindFirstChild('QueueEvent')
local MainEvent = GameRemotes:FindFirstChild('MainEvent')
-- Values --
local BlueDebounce = false
local RedDebounce = false
----

-- Functions --
local function QueueGame()
	-- For Statement --
	for _,QueuePad in QueuingFolder:GetChildren() do
		if string.sub(QueuePad.Name,1,4) == 'Team' and QueuePad.Parent == QueuingFolder then
			-- On Touched --
			QueuePad.Touched:Connect(function(BasePart)
				local CurrentPad = QueuePad
				-- Check --
				if not BasePart.Parent:FindFirstChild('Humanoid') or not BasePart.Parent:FindFirstChild('HumanoidRootPart') or not QueuePad:GetAttribute('Queueable',false) then return end
				local Humanoid = BasePart.Parent:FindFirstChild('Humanoid')
				local HumanoidRootPart = BasePart.Parent:FindFirstChild('HumanoidRootPart')
				-- Update --
				Humanoid.WalkSpeed = 0
				Humanoid.JumpPower = 0
				HumanoidRootPart.CFrame = CFrame.new(QueuePad.Position.X,HumanoidRootPart.Position.Y,QueuePad.Position.Z)
				HumanoidRootPart.Orientation = Vector3.new(0,-90,0)
				QueuePad:SetAttribute('QueuedPlr',BasePart.Parent.Name)
				QueuePad:SetAttribute('Queueable',false)
				-- Fire Event --
				QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Humanoid.Parent),'ShowLeave')
				-- Hop Off Pad --
				local Connection
				Connection = Humanoid:GetPropertyChangedSignal('Jump'):Connect(function()
					-- Check --
					if Humanoid.WalkSpeed == 0 and not CameraFolder.Parent:GetAttribute('GameStarted',true) then
						Humanoid.WalkSpeed = 16
						Humanoid.JumpPower = 50
						-- Update --
						CurrentPad:SetAttribute('QueuedPlr','')
						task.wait(2)
						CurrentPad:SetAttribute('Queueable',true)
					end
					----
					QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Humanoid.Parent),'HideLeave')
					Connection:Disconnect()
				end)
			end)
		end
	end
end
---
local function PlrTurn(Char1Turn,Character1,Character2)
	-- Check --
	if Char1Turn == true then
		-- Update Game --
		Character1:SetAttribute('Slapping',true)
		Character2:SetAttribute('Slapping',false)
		MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Character1),'Slapping')
		MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Character2),'GetSlapped')
	elseif Char1Turn == false then
		-- Update Game --
		Character1:SetAttribute('Slapping',false)
		Character2:SetAttribute('Slapping',true)
		MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Character1),'GetSlapped')
		MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Character2),'Slapping')
	end
end
---
local function EndGame()
	-- Character / Constants --
	local Char1 = workspace:FindFirstChild(QueuingFolder.TeamBlue:GetAttribute('QueuedPlr'))
	local Char2 = workspace:FindFirstChild(QueuingFolder.TeamRed:GetAttribute('QueuedPlr'))
	-- Find First Knocked / Check --
	if Char1:GetAttribute('Health') == 0 then
		-- Update Values --
		Char1:SetAttribute('Slapping',false)
		Char2:SetAttribute('Slapping',false)
	end
end
---
local function NewStartGame()
	-- Check Update --
	CameraFolder.Parent:GetAttributeChangedSignal('GameStarted'):Connect(function()
		if CameraFolder.Parent:GetAttribute('GameStarted',true) then
			-- Setup Game --
			local Char1 = workspace:FindFirstChild(QueuingFolder.TeamBlue:GetAttribute('QueuedPlr'))
			local Char2 = workspace:FindFirstChild(QueuingFolder.TeamRed:GetAttribute('QueuedPlr'))
			-- Constants / Camera --
			local IntroCam1 = CameraFolder:FindFirstChild('IntroCam1')
			local IntroCam2 = CameraFolder:FindFirstChild('IntroCam2')
			local RedCamera = CameraFolder:FindFirstChild('RedCamera')
			local BlueCamera = CameraFolder:FindFirstChild('BlueCamera')
			-- Call Events --
			task.wait(0.2)
			MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),{IntroCam1,BlueCamera},'To IntroCamera')
			MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),{IntroCam2,RedCamera},'To IntroCamera')
			QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'HideLeave')
			QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'HideLeave')
			-- Teleport --
			Char1.HumanoidRootPart.CFrame = CFrame.new(FloorModel.Blue.Position.X,Char1.HumanoidRootPart.Position.Y,FloorModel.Blue.Position.Z)
			Char1.HumanoidRootPart.Orientation = Vector3.new(0,180,0)
			Char2.HumanoidRootPart.CFrame = CFrame.new(FloorModel.Red.Position.X,Char2.HumanoidRootPart.Position.Y,FloorModel.Red.Position.Z)
			Char2.HumanoidRootPart.Orientation = Vector3.new(0,0,0)
			-- Functions --
			local function SetupGame()
				Char1:SetAttribute('Slapping',false)
				Char2:SetAttribute('Slapping',false)
				-- Make It Pads Not Getonable --
				local RedPad = QueuingFolder:FindFirstChild('TeamRed')
				local BluePad = QueuingFolder:FindFirstChild('TeamBlue')
				-- Update --
				RedPad:SetAttribute('Queueable',false)
				BluePad:SetAttribute('Queueable',false)
			end
			-- Continue --
			SetupGame()
			task.wait(3)
			PlrTurn(true,Char1,Char2)
			-- On Touched / Char1 --
			Char1.Head.Touched:Connect(function(BasePart)
				if BasePart.Parent:FindFirstChild('Humanoid') and BasePart.Parent:IsA('Model') and BasePart.Name == 'RightHand' then
					-- Regardless Shake Screen --
					MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'ShakeScreen')
					-- Check --
					if Char1.Humanoid.Health == 0 then
						EndGame()
					else
						Char1.Humanoid.Health =- 20
						-- Reverse Roles --
						task.wait(5)
						PlrTurn(false,Char1,Char2)
					end
				end
			end)
			-- On Touched / Char2 --
			Char2.Head.Touched:Connect(function(BasePart)
				if BasePart.Parent:FindFirstChild('Humanoid') and BasePart.Parent:IsA('Model') and BasePart.Name == 'RightHand' then
					-- Regardless Shake Screen --
					MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'ShakeScreen')
					-- Check --
					if Char2.Humanoid.Health == 0 then
						EndGame()
					else
						Char2.Humanoid.Health =- 20
						-- Reverse Roles --
						task.wait(5)
						PlrTurn(true,Char1,Char2)
					end
				end
			end)
		end
	end)
	-- Pads / Constants --
	local BluePad = QueuingFolder:FindFirstChild('TeamBlue')
	local RedPad = QueuingFolder:FindFirstChild('TeamRed')
	-- Check Changed / Blue --
	BluePad:GetAttributeChangedSignal('QueuedPlr'):Connect(function()
		if BluePad:GetAttribute('QueuedPlr') ~= '' and RedPad:GetAttribute('QueuedPlr') ~= '' then
			-- Update --
			CameraFolder.Parent:SetAttribute('GameStarted',true)
		end
		-- Check --
		if RedPad:GetAttribute('QueuedPlr') == '' then
			CameraFolder.Parent:SetAttribute('GameStarted',false)
		end
	end)
	-- Check Changed / Red --
	RedPad:GetAttributeChangedSignal('QueuedPlr'):Connect(function()
		if BluePad:GetAttribute('QueuedPlr') ~= '' and RedPad:GetAttribute('QueuedPlr') ~= '' then
			-- Update --
			CameraFolder.Parent:SetAttribute('GameStarted',true)
		end
		-- Check --
		if BluePad:GetAttribute('QueuedPlr') == '' then
			CameraFolder.Parent:SetAttribute('GameStarted',false)
		end
	end)
	---- Check Updates --
	--Char1:GetAttributeChangedSignal('Health'):Connect(function()
	--	if Char1:GetAttribute('Health') == 0 then
	--		PlrTurn(false,Char1,Char2)
	--	end
	--end)
	-----
	--Char2:GetAttributeChangedSignal('Health'):Connect(function()
	--	if Char2:GetAttribute('Health') ~= 0 then
	--		PlrTurn(true,Char1,Char2)
	--	end
	--end)
	-- On Touched --
	--Char1:FindFirstChild('Hitbox').Touched:Connect(function(BasePart)
	--	if BasePart.Parent:FindFirstChild('Humanoid') and BasePart.Parent:IsA('Model') and BasePart.Name == 'RightHand' then
			
	--	end
	--end)
end
----

-- Init --
QueueGame()
NewStartGame()
----

– Client

-- Services --
local PlrService = game:GetService('Players')
local RunService = game:GetService('RunService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
-- Constants / Normal --
local Character = script.Parent
local LocalPlr = PlrService:GetPlayerFromCharacter(Character)
local LocalMouse = LocalPlr:GetMouse()
-- Constants / Remotes --
local GameRemotes = ReplicatedStorage:FindFirstChild('GameRems')
local MainEvent = GameRemotes:FindFirstChild('MainEvent')
-- Constants / Anims --
local GetStanding = Character.Humanoid:LoadAnimation(GameRemotes:FindFirstChild('GetStand'))
local Standing = Character.Humanoid:LoadAnimation(GameRemotes:FindFirstChild('Stand'))
----

-- OnClientEvent --
MainEvent.OnClientEvent:Connect(function(Action)
	-- Check --
	if Action == 'Slapping' then
		-- Play Animation --
		Standing:Stop()
		-- While Statement --
		while Character:GetAttribute('Slapping',true) do
			RunService.Heartbeat:Wait()
			-- Fire Event --
			MainEvent:FireServer('Slapping',LocalMouse.Hit.X)
		end
		-- Check --
	elseif Action == 'GetSlapped' then
		-- Play Animation --
		GetStanding:Play()
		GetStanding.Stopped:Wait()
		Standing:Play()
	end
end)
----

Not possible to figure out whats going on with this amount of information. Only thing I can think of is that you’re sending a remote event every frame when slapping is true.

Would like a video of it help better?

Not especially. You need ways to time how long each thing is running for. You can use tick() to get an accurate time and find a way to measure delay like that. If the bottleneck is between client and server you might have to come up with something trickier. Otherwise you could try turning different parts off and seeing what has the biggest impact on speed.

1 Like