Help with lag/server crashing when selling items

  1. What do you want to achieve? I want my general store to work, without lag and server crashing

  2. What is the issue? When I try to sell items the ping gets really high, and everything just stops. If I sell enough items the whole server crashes.

  3. What solutions have you tried so far? I’ve been trying to figure it out on my own by debugging the code. I’ve also tried to ask for help in a discord server but nobody was able to help there.

Here is the Server Script:

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStore2 = require(ServerScriptService:WaitForChild("DataStore2"))
local Players = game:GetService("Players")

local ToolsFolder = ReplicatedStorage:WaitForChild("Tools")

local remotes = ReplicatedStorage:WaitForChild("Remotes")
local UI_Remotes = remotes:WaitForChild("UI")
local BuySellItemEvent = remotes:WaitForChild("BuySellItem")

local NotifyEvent = UI_Remotes:WaitForChild("Notify")

local ItemsModule = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("ItemList"))

local DebouncePlayers = {}

local function GetTool(mode, toolInfo, player)
	local tools
	if mode == false then
		tools = ToolsFolder:GetChildren()
	elseif mode == true then
		tools = player.Backpack:GetChildren()
	end

	for _, tool in ipairs(tools) do
		if tool.Name == toolInfo or tool.InventorySlot.Value == toolInfo then
			return tool
		end
	end

	return nil
end

local function GetValues(ItemName)
	local storeList = ItemsModule.StoreList.General_Store
	local itemData = storeList[ItemName]
	if itemData then
		return itemData.SellValue, itemData.BuyValue
	end
	return nil
end

local function BuySellTool(player, InfoTable)
	if DebouncePlayers[player.UserId] then
		return false
	end

	DebouncePlayers[player.UserId] = true
	task.delay(0.1,function()
		DebouncePlayers[player.UserId] = false
	end)

	local moneyDataStore = DataStore2("money", player)
	local Tools = player.Backpack

	local moneyToAdd = 0
	local ownedMoney = moneyDataStore:Get()

	for i, v in ipairs(InfoTable) do
		local mode = v.mode
		local toolInfo = v.ToolInfo

		local item = GetTool(mode, toolInfo, player)

		if item then
			local SellValue, BuyValue = GetValues(item.Name)

			if SellValue and BuyValue then
				if mode then
					task.spawn(function()
						if SellValue > 0 then
							moneyToAdd += SellValue
							ownedMoney += SellValue
						end

						item.Stack.Value -= 1
						if item.Stack.Value == 0 then
							item:Destroy()
						end
					end)
				else
					local toolToBuy = Tools:FindFirstChild(toolInfo)

					if toolToBuy then
						local Stack = toolToBuy:FindFirstChild("Stack")
						local MaxStack = toolToBuy:FindFirstChild("MaxStack")

						if Stack and MaxStack and Stack.Value < MaxStack.Value then
							if ownedMoney >= BuyValue then
								ownedMoney -= BuyValue
								moneyToAdd += BuyValue
								Stack.Value += 1
							end
						elseif #Tools:GetChildren() < 25 then
							if ownedMoney >= BuyValue then
								local newItem = item:Clone()
								newItem.ToolOwned.Value = true
								newItem.InventorySlot.Value = ""
								newItem.Stack.Value = 1
								newItem.Parent = Tools
								ownedMoney -= BuyValue
								moneyToAdd += BuyValue
							end
						end
					elseif #Tools:GetChildren() < 25 then
						if ownedMoney >= BuyValue then
							local newItem = item:Clone()
							newItem.ToolOwned.Value = true
							newItem.InventorySlot.Value = ""
							newItem.Stack.Value = 1
							newItem.Parent = Tools
							ownedMoney -= BuyValue
							moneyToAdd += BuyValue
						end
					end
				end
			end
		end
	end

	moneyDataStore:Increment(moneyToAdd)
end

local function setup()
	BuySellItemEvent.OnServerEvent:Connect(BuySellTool)
end

setup()
  1. Some further information. The remote event is sent from the client once I close the General Store Gui. The server recieves a table which contain each action. Within each action there is a bit of information, and that is; Mode(Sell/Buy) and ToolInfo(Tool name/Inventoryslot of tool) I have made a custom inventory system that uses stack,maxstack,inventoryslot etc to determine the places and other stuff.

I would assume it is because of the DataStores. It looks like every time you sell an item, you get a value and then set a value. You really want to use DataStores as sparingly as possible.

It stills sounds really extreme for it to crash the server, but either way, you need to reduce the DataStore requests.

1 Like

As of now I only acquire the current value and set it once per remote event, so I doubt that’s the problem. The weird thing is that when I buy items nothing lags out, even though there’s a lot more to check when buying items…

Even if it isn’t the DataStores causing the problems, you still need to drastically reduce the amount of requests per minute. The Roblox servers has a limit on each game, and reaching the limit will start delaying further requests.