Issues with major lag spikes all throughout my game

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I need help with fixing all the lag issues in my game, I’m not asking for complete code, I’m just asking for what the issues are and how to help it.

  2. What is the issue? Include screenshots / videos if possible!
    Basically there are 2 main issues, whenever I kill a player or npc in my game, the entire client and server pauses and makes my gpu fans go up. Another issue is when I equip the weapons in the game, the game will pause in the client for like 2-5 seconds (it only happens when I have viewmodels enabled).

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried to just optimise as much as I can but I don’t have much knowledge on optimising games.

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!

Server code for hitting the player

local push = function(dir,p) 
	local Vele = Instance.new("BodyVelocity",p) 
	Vele.MaxForce = Vector3.new(1,1,1) * math.huge 
	Vele.Velocity = dir*10
	debris:AddItem(Vele,.1)
end

local hitPlayer = function(piece,damage,dir,origin,pos,character,p)
	table.insert(character,piece.Parent)
	for i,v in pairs(workspace.walls:GetChildren()) do
		table.insert(character,v)
	end
	for i,v in pairs(workspace.doors:GetChildren()) do
		table.insert(character,v)
	end
	for i,v in pairs(workspace:GetChildren()) do
		if not v:FindFirstChildWhichIsA("Humanoid") then continue end
		for _,b in pairs(v:GetChildren()) do
			if not b:IsA("Accessory") then continue end
			table.insert(character,b)
		end
	end
	for i,v in pairs(workspace.rappel:GetChildren()) do
		if not v:IsA("BasePart") then continue end
		for _, b in pairs(v:WaitForChild("window"):GetChildren()) do
			if not v:IsA("BasePart") then continue end
			table.insert(character,b)
		end
	end
	if piece.Name == "Head" then
		piece.Parent:FindFirstChildWhichIsA("Humanoid"):TakeDamage(damage*10)
		clientEvs.hit:FireClient(p,0,1)
	elseif piece.Name ~= "Torso" and piece.Name ~= "HumanoidRootPart" then
		piece.Parent:FindFirstChildWhichIsA("Humanoid"):TakeDamage(damage*.75)
		clientEvs.hit:FireClient(p,0,0)
	else
		piece.Parent:FindFirstChildWhichIsA("Humanoid"):TakeDamage(damage)
		clientEvs.hit:FireClient(p,0,0)
	end
	if piece.Parent:FindFirstChildWhichIsA("Humanoid") and piece.Parent:FindFirstChildWhichIsA("Humanoid").Health <= 0 then
		push(dir,piece)
	end
end

--in an event connect that is fired when the player gets a hit on the client
if hit then
		if hit.Parent:IsA("Accessory") and hit.Parent.Parent:FindFirstChildWhichIsA("Humanoid") or hit.Parent:FindFirstChildWhichIsA("Humanoid") then
			local filter = {}
			hitPlayer(hit,realdamage,dir,origin,position,filter,p)
		else
			if hit.Anchored == false then
				push(dir,hit)
			end
		end
	end

this is the client code for the vms

local removeVM = function()
	debrisGobbler:AddItem(vm,0)
	animations = animationsTemp
	vm = nil
end

local addVM = function(name)
	if vm then
		removeVM()
	end
	vm = game.ReplicatedStorage.VMS[name]:Clone()
	aimVector = (vm.aimPart.Position-vm.HumanoidRootPart.Position)
	aimVectorX = vm.aimPart.Position.X-vm.HumanoidRootPart.Position.X
	vm.Parent = workspace.CurrentCamera
	for _,v in pairs(vm:FindFirstChild("Animations"):GetChildren()) do
		loadAnimation(v.AnimationId,v.Name,animations,vm.Humanoid)
	end
end

-- this is in a renderstepped function
if game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool") then
		if itemEquipped ~= game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool") then
			itemEquipped = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool")
			addVM(itemEquipped.Name)
		end
	else
		if itemEquipped then
			itemEquipped = nil
			removeVM()
		end
	end

	if game.Players.LocalPlayer.Character.Humanoid.Health <= 0 then
		removeVM()
		ermWadda:Disconnect()
	end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Thanks if you guys can help me at all because I really am sort of done with this at this point.

1 Like

I can’t see anything (in my very untrained eye) that would create a lot of lag, in exactly these scripts.

Do you have any other scripts that may have a lot of loops, or doing a lot of stuff very quickly, generally? Unanchored parts (many things calculating at the same time causes lag)? Etc?

I am sure we need more info to troubleshoot.

1 Like

I know its a very bad thing to do but most of the scripts in the game use some sort of loop because they sort of need it.

I can send the entirety of the client script for the VM’s if you think it could possibly help with figuring out the issues. (I feel like the VM’s lag is the most important issue right now and people could possibly deal with lag on kills until I fix it)

1 Like

Here is a video of my issue by the way:

Ok so your right, it isn’t to do with the client scripts on either or the server. For some reason my destructible walls setup script. What I don’t get is that it doesn’t have any loops except for the loop that spawns all the parts on the walls.

1 Like

I’m a bit as stumped as you are. I noticed that there was mostly just lagspikes when you were hitting a rig, apparently you have something that may create a LOT of effects at the same time when that happens.

Try maybe disabling any function that plays when you hit rigs/parts? Maybe it has something to do with the firing of the weapon?

1 Like

Basically all that happens when you kill the player/rig, it ragdolls and applies a force so that the ragdoll doesn’t just stand up facing down. The thing I’m trying to figure out though is that when I disable my walls script that spawns all the parts for the walls, none of the issues happen at all and the game runs smoothly and with no stutters at all.

local walls = workspace.walls
local evs = game.ReplicatedStorage.evs
local debrisGobbler = require(game.ServerScriptService.modules.DebrisGobbler.DebrisGobbler)

local defaultsize = Vector3.new(1,1,1)

local setupWall = function(w)
	game.ReplicatedStorage.currentGame.loadingStatus.walls.amount.Value += 30+40
	local objectValue = Instance.new("ObjectValue")
	objectValue.Value = w
	objectValue.Parent = walls
	objectValue.Name = w.Name
	local instance1 = Instance.new("Part")
	instance1.Anchored = true
	instance1.Size = Vector3.new(w.Size.X/30,w.Size.Y/40,w.Size.Z)
	instance1.Parent = objectValue
	instance1.CFrame = w.CFrame*CFrame.new(-w.Size.X/2+(instance1.Size.X/2),w.Size.Y/2-(instance1.Size.Y/2),0)
	instance1.Color = w.Color
	instance1.Material = w.Material
	local num = 0
	repeat 
		task.wait()
		local next = instance1:Clone()
		next.CFrame = next.CFrame*CFrame.new(instance1.Size.X*num,0,0)
		next.Parent = objectValue
		num = num+1
		game.ReplicatedStorage.currentGame.loadingStatus.walls.loaded.Value += 1
	until num == 30
	local num = 0
	local instance2 = objectValue:GetChildren()
	repeat
		task.wait()
		for i,v in pairs(instance2) do
			local next = v:Clone()
			next.CFrame = next.CFrame*CFrame.new(0,-instance1.Size.Y*num,0)
			next.Parent = objectValue
		end
		num = num+1
		game.ReplicatedStorage.currentGame.loadingStatus.walls.loaded.Value += 1
	until num == 40
	w.Transparency = 1
	local folder = Instance.new("Folder")
	folder.Parent = objectValue.Value
	folder.Name = "places"
	local p1 = Instance.new("Part")
	local p2 = Instance.new("Part")
	p1.Anchored = true
	p2.Anchored = true
	p1.CanCollide = false
	p2.CanCollide = false
	p1.CanQuery = false
	p2.CanQuery = false
	p1.CanTouch = false
	p2.CanTouch = false
	p1.Parent = folder
	p2.Parent = folder
	p1.CFrame = objectValue.Value.CFrame*CFrame.new(0,0,1.793999999999997)
	p2.CFrame = objectValue.Value.CFrame*CFrame.new(0,0,-1.793999999999997)*CFrame.Angles(0,math.rad(180),0)
	p1.Name = "p1"
	p2.Name = "p2"
	p1.Size = defaultsize
	p2.Size = defaultsize
	p1.Transparency = 1
	p2.Transparency = 1
end

local reinforce = function(w)

end

for i,v in pairs(walls:GetChildren()) do
	setupWall(v)
end

evs.reinforce.OnServerEvent:Connect(function(p,w)
	w.Name = "reinforced"
	w.Material = Enum.Material.DiamondPlate
	w.Color = Color3.fromRGB(255, 255, 255)
	w:ClearAllChildren()
	for i,v in pairs(walls:GetChildren()) do
		if not v:IsA("ObjectValue") then continue end
		if v.Value == w then
			v:Destroy()
		end
	end
	setupWall(w)
end)

evs.wallsRemove.OnServerEvent:Connect(function(p,parts)
	table.foreach(parts,function(_,b)
		if b:IsA"BasePart" then
			b.CFrame = b.CFrame*CFrame.new(-10000,-10000,-10000)
			b.Transparency = 1
			b.CanCollide = false
			b.CanQuery = false
			b.CanTouch = false
			coroutine.resume(coroutine.create(function()
				debrisGobbler.AddItem(b,0)
			end))
		end
	end)
end)

This is the script for my walls but I have no idea what the issue could be.

Edit: The player/rig kills causes lag because of the BreakJointsOnDeath thing in the humanoid

1 Like

what immediately stands out to me is that you loop through workspace on each hit, and long loops like that definitely cause lag(especially on the server, since it is less powerful)

Do you know how I could make a work around for this as I need mainly the accessory one so that the player cant shoot a large accessory handle and have it count as a hit.

The only thing I’ve really figured out though is that the issues arise whenever there is something to do with adding or removing instances, I had the same issue a bit ago with adding bullet holes and them just completely killing the game when shooting. I ended up just removing them as I have 0 idea how to fix it.

1 Like

you can use a script that turns off canQuery for all the accessories’ handles’ when a player spawns in, so that no projectile(.Touched, raycasts) can hit it

1 Like

This worked perfectly.

What I did to fix all my problems is:

VMS causing lag, I preloaded the viewmodel’s to the players camera and swapped which was used in the script.

Players/rig kills causing lag, I removed the ragdoll script and turned off breaking joints on death and it made it not lag anymore.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.