Lag spike when equipping an item

I’m making an fps system

The issue with this is that I get a big lag spike when ever I equip the weapon.

I have asked multiple people for help and tried different ways of going for this but still no luck.

function module:Equip(name)
	
	if CurrentWeapon and CurrentWeaponSettings then
		CurrentWeapon:Destroy()
		CurrentWeaponSettings = nil
		CurrentWeapon = nil
	end
	
	if not WeaponSwitchingDebounce then
		WeaponSwitchingDebounce = true
		
		local weapon = FrameworkFolder.Viewmodels:FindFirstChild(name):Clone()
		CurrentWeaponSettings = require(FrameworkFolder.Modules.Weapon:FindFirstChild(name.."Setup"))
		CurrentWeapon = weapon
		CurrentWeapon.Parent = Camera
		waitTime(.25)
		
		WeaponSwitchingDebounce = false
	end
end

Can you please provide the Weapon module? The problem maybe the Setup function or it may happen when you clone your Viewmodels because high amount of meshes.

This is more than likely due to the clone.


Here is an image of what the viewmodels look like. Would there be any other way of cloning the viewmodel better?

This is inside of the local script I am using (the equip function is in a module)

-----------------------------------
repeat wait() until game:IsLoaded()
-----------------------------------
-----------------------------------
local Player = game.Players.LocalPlayer
local Character = game.Players.LocalPlayer.Character
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
-----------------------------------
-----------------------------------
local ReplicatedStorage = game.ReplicatedStorage
local IndigoFolder = game.ReplicatedStorage.Indigo
local FrameworkFolder = game.ReplicatedStorage.Indigo.Framework
-----------------------------------
-----------------------------------
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
-----------------------------------
-----------------------------------
local Shared = require(script.SharedScripts)
local Indigo = require(FrameworkFolder.Modules.Used.IndigoEngine)
-----------------------------------
-----------------------------------

UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.One then
		Indigo:Equip(Shared.Loadout.Primary)
	end
end)

RunService.RenderStepped:Connect(function()
	Indigo:Update()
end)

I think instead of cloning, you can change it parent and then change it back. And make sure change it on Client side so glitch won’t happen for everyone

I just tried that and still a lag spike. I really have no clue what could be causing this. (Updated code):

function module:Equip(name)
	if CurrentWeapon then
		CurrentWeapon.Parent = FrameworkFolder.Viewmodels
		CurrentWeapon = nil
		CurrentWeaponSettings = nil
	end
		
	local weapon = FrameworkFolder.Viewmodels:FindFirstChild(name)
	CurrentWeaponSettings = require(FrameworkFolder.Modules.Weapon:FindFirstChild(name.."Setup"))
	CurrentWeapon = weapon
	CurrentWeapon.Parent = Camera
end

On, I think you shouldn’t run the loop like that. Instead, you can use game.Loaded:Wait() for more efficiency.

1 Like

Alright so the lag spike is not as severe as it was, but its still noticeable. I have had this issue for around a year now. I still do not know why this is happening.

I guess it’s your device issues? Maybe you can try to publish the game and try it on different devices.

I’ll see. Here is a video to show this with stats.