What are the factors that cause my Game becomes so Laggy?

Are the buildings only meant to break when certain conditions are met? I can destroy the blue’s base, but not the pink one even when I switch teams.

Running inefficient scripts can certainly make your game lag as mentioned in previous replies. One tool that I suggest you take advantage of is the Script Performance widget. To open this, go to the View > Script Performance. Run your game and take a look at the Activity values in the window. Generally speaking, higher percentages are not good. The widget should identify which scripts are producing these high activity rates, and you can optimize these scripts accordingly.

1 Like

Hi herbz i just updated the game . Would you like to test it again im really stuck on this. The mechanics of the game by the way is to excort your Teams cube to the other side until it touches the Goal which the Part in Neon Material then afther that you can finally destyor the enemy’s base until no Spawn location left

1 Like

I played it again, and I think you might be correct. The lag is being created when buildings are destroyed through the script. This is demonstrated through, when the script isn’t activated (when I can’t destroy the buildings) and there doesn’t seem to be any lag even when there is more than one player on the server. See if that is the same for you.

If the lag is still an issue when you destroy the buildings, try this script instead maybe:

db = false

while task.wait() do
	local Base = game.Workspace:WaitForChild("Base")
	local BlueCube = game.Workspace:WaitForChild("BlueCube")
	local PurpleCube = game.Workspace:WaitForChild("PurpleCube")
	BlueCube.Touched:Connect(function(hit)
		if hit:IsA("BasePart") and hit.Name == "PurpleGoal" and hit.BrickColor == BrickColor.new("Pastel violet") then
			task.wait()
			for i,v in pairs(Base:GetDescendants()) do
				if v:IsA("BasePart") and v.Name == "-Purple" or v:IsA("SpawnLocation") and v.BrickColor == BrickColor.new("Pastel violet") then
					v.Anchored = false
					hit.Transparency = 1
					BlueCube:Destroy()
				end
			end
		elseif hit.Name == "Water" and hit.Transparency == 1 then
			BlueCube.Position = Vector3.new(-54.5, 4.559, 51.5)
			BlueCube.Orientation = Vector3.new(0,0,0)
			BlueCube.Anchored = true
			task.wait(5)
			BlueCube.Position = Vector3.new(-54.5, 4.559, 51.5)
			BlueCube.Orientation = Vector3.new(0,0,0)
			BlueCube.Anchored = false
		end
	end)
	-------------------------------------------------------------------------------------------------	
	PurpleCube.Touched:Connect(function(hit)
		if hit:IsA("BasePart") and hit.Name == "BlueGoal" and hit.BrickColor == BrickColor.new("Bright blue") then
			task.wait()
			for i,v in pairs(Base:GetDescendants()) do
				if v:IsA("BasePart") and v.Name == "-Blue" or v:IsA("SpawnLocation") and v.BrickColor == BrickColor.new("Bright blue") then
					v.Anchored = false
					hit.Transparency = 1
					PurpleCube:Destroy()
				end
			end
		elseif hit.Name == "Water" and hit.Transparency == 1 then
			PurpleCube.Position = Vector3.new(-483.5, 4.561, 19.5)
			PurpleCube.Orientation = Vector3.new(0,0,0)
			PurpleCube.Anchored = true
			wait(3)
			PurpleCube.Position = Vector3.new(-483.5, 4.561, 19.5)
			PurpleCube.Orientation = Vector3.new(0,0,0)
			PurpleCube.Anchored = false
		end
	end)
	-------------------------------------------------------------------------------------
	local Dirt = game.Workspace:WaitForChild("Dirt")
	local SolidDirt = game.Workspace:WaitForChild("SolidDirt")
	for i,v in pairs(Dirt:GetChildren()) do
		task.wait()
		if v:IsA("BasePart") then
			v.Touched:Connect(function(hit)
				task.wait()
				if hit.Name == "HitBox" and not db and hit.Size == Vector3.new(0.31, 0.512, 0.273) then
					db = true
					v.Transparency = v.Transparency + 0.2
					wait(0.1)
					db = false
				elseif hit.Name == "HitBox2" and not db and  hit.Size == Vector3.new(0.31, 0.512, 0.273) then
					db = true
					v.Transparency = v.Transparency + 1
					wait(0.1)
					db = false
				end
			end)
		end
	end
	for i,v in pairs(SolidDirt:GetChildren()) do
		task.wait()
		if v:IsA("BasePart") then
			v.Touched:Connect(function(hit)
				if hit.Name == "HitBox2" and not db and  hit.Size == Vector3.new(0.31, 0.512, 0.273) then
					task.wait()
					db = true
					v.Transparency = v.Transparency + 0.2
					wait(0.1)
					db = false
				end
			end)
		end
	end
end

I added more of task.wait() and used it instead of wait(). That could’ve been the cause behind it, but see if that changes anything.

@IvanoZ, has it worked? Mark as solution if it has.

1 Like

Thank you for your effort Herbz. I’ll to do.the things you had suggested on me

1 Like

I Finally fixed The Lag. I just remove the Touch Event of the Baseplate from the While Loop.

1 Like