This part of code makes lag spikes and i dont know why

Hello, this is a part of my FPS framework, Drop and holster functions (server side)

local function Drop(tool)
	wait(0.1)
	print(game.ReplicatedStorage.LunarWeaponary.ViewModels:GetChildren())
	local drop = game.ReplicatedStorage.LunarWeaponary.ViewModels["v_"..tool.Name]:Clone()
	print(drop)
	for i,v in pairs(drop:GetChildren()) do
		if v.Name == "Left Arm" then
			v:Destroy()
		elseif v.Name == "Right Arm" then
			v:Destroy()
		elseif v.Name == "HumanoidRootPart" then
			v:Destroy()
		end
	end
	for i,v in pairs(drop:GetChildren()) do
		spawn(function()
			v.CollisionGroup = "Default"
			v.CanCollide = true
			v.CanQuery = true
			v.Massless = false
		end)
	end
	drop.PrimaryPart = drop.Grip
	player.Character:WaitForChild("Remove"):FireClient(player,tool)
	drop.Parent = workspace
	drop:SetPrimaryPartCFrame(player.Character.Torso.CFrame * CFrame.new(Vector3.new(0,0,-2)))
	drop.Grip.Velocity = player.Character.Torso.CFrame.lookVector * 20
	print("Dropped")
end
local function Holster(plr,tool)
	for i,v in pairs(plr.Character.Holsters:GetChildren()) do
		if v.Name == tool.Name then
			v:Destroy()
		end
	end
	local drop
	if plr.Character:FindFirstChild("v_"..tool.Name) then
		local var = plr.Character["v_"..tool.Name]:Clone()
		drop = var.Handle.Gun
	else
		drop = game.ReplicatedStorage.LunarWeaponary.ViewModels["v_"..tool.Name]:Clone()
	end

	print(drop)
	for i,v in pairs(drop:GetChildren()) do
		if v.Name == "Left Arm" then
			v:Destroy()
		elseif v.Name == "Right Arm" then
			v:Destroy()
		elseif v.Name == "HumanoidRootPart" then
			v:Destroy()
		end
	end
	local weld = Instance.new("WeldConstraint")
	drop.PrimaryPart = drop.Grip
	drop:SetPrimaryPartCFrame(player.Character.Torso.CFrame * CFrame.new(Vector3.new(0.5,0,0.6)) * CFrame.Angles(math.rad(-90),math.rad(45),math.rad(90)))
	weld.Parent = drop
	weld.Part0 = drop.PrimaryPart
	weld.Part1 = player.Character.HumanoidRootPart
	drop.Parent = plr.Character.Holsters
	drop.Name = tool.Name
	svm:Destroy()
end

the problem is that whenever i drop or holster the weapon the game lags

if anyone have any idea how to make it less laggy please let me know, any help is appreciated

Are you sure it’s actually lagging and not just the wait(0.1) you put there?

1 Like

yes, it lags even without wait(0.1)

Could you show the script that calls these functions?

Creating a new thread is a lot of overhead for code that doesn’t need to be asynchronous. I guarantee this harms more than it helps.

I don’t think that would be enough to cause noticable lag though.

1 Like

One thing to keep in mind is, adding arbitrary waits to your code will never fully resolve your performance issues.

Reading your code, nothing significant stands out to me. Have three questions for you to possibly look into:

  1. How large are the models you are modifying?
  2. Where/how often are these functions being called?
  3. Why are you making a new thread thru spawn() for every child in the model? Operations such as setting properties aren’t things that require being run outside of the main scope
  1. Models are not that big
  2. only when equiping,unequiping,removing and adding to inventory
  3. I removed the spawn() but the lag is same