Can this code lag when used by a lot of players?

So recently i’m working on inventory system, the hardest one in my entire game, and the thing is that i’m scared of memory leaks ect. game will be based around it and one module is crucial for it to work

local Item = {}

-- Settings
local MaxPickupDistance = 24
local DropOffset = Vector3.new(0,-2,-5)

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Important stuff
local remote = ReplicatedStorage.Remotes.PickAndDrop
local Inventories = ReplicatedStorage.Inventories
local ItemData = require(ReplicatedStorage.ObjectsData.ItemsData)

-- Class to folder
local ClassInfo = {
	["Tool"] = {
		["Folder"] = "SingleItems",
		["ValueType"] = "BoolValue"
	},
	["Item"] = {
		["Folder"] = "Inventory",
		["ValueType"] = "IntValue"
	},
	["Food"] = {
		["Folder"] = "Inventory",
		["ValueType"] = "IntValue"
	}
}
-- Drop item in front of player
function Item:Drop(item: ValueBase, CharacterCFrame: Vector3)
	local name = item.Name
	if item:IsA("IntValue") then 
		item.Value -= 1 
		if item.Value <= 0 then item:Destroy() end
	else
		item:Destroy()
	end
	
	local newModel = game:GetService("ServerStorage").ItemModels[name]:Clone()
	newModel.Name = name
	newModel.CFrame =  CharacterCFrame * CFrame.new(DropOffset)
	newModel.Parent = workspace.Items
end

-- Pick item if it's in range of player
function Item:Pick(item: BasePart, player: Player)
	if (item.Position - player.Character.PrimaryPart.Position).Magnitude > MaxPickupDistance then return end
	
	local name = item.Name
	
	local Class = ItemData[name].Class
	local FolderName = ClassInfo[Class].Folder
	local ValueType = ClassInfo[Class].ValueType
	local Folder = Inventories[player.UserId][FolderName]
	
	if not Folder:FindFirstChild(name) or FolderName == "SingleItems" then 
		local NewValue = Instance.new(ValueType)
		NewValue.Name = name
		
		if ValueType == "IntValue" then NewValue.Value = 1 else NewValue.Value = false end
		NewValue.Parent = Folder
	else
		Folder[name].Value += 1 	
	end
	
	item:Destroy()
end

return Item

What it does is drops or picks item that was checked by Main script, yk debounce, if it’s real or if it’s actual item, not other random part.

At the end, will this code lag if used by a lot of players, take in mind that there is 0.1 second debounce each time player can drop or pick an item

There’s no reason for it to lag, you’re good.

1 Like

I think i could done something better, i have many checks and player can drop a lot of items

Your script puts very little stress on the server, it’s a good practice to put a lot of checks in a function connected to your remove event.

The only thing that could lag (locally) is when someone throws away too many items and each of them has some resource-intensive animation.

If you want to see how performant your code is, take this function and call it 1000 times each frame. It will run smoothly.

1 Like

I made 1000 remote requests, 60 per second, the most lag was physical parts, but with debounce of 0.2 to drop or pick item i think it will don’t lag much, thanks for help

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