Big Lag Spikes [Breakable System]

Hi devs, so i have been experiencing this huge lag spike problem. And i’m here to know how i can prevent this issue. I hope i can fix this problem as much as possible!

1 Like

Here’s the full code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")

local client_player = Players.LocalPlayer

local shared_modules = ReplicatedStorage:WaitForChild("SharedModules")
local configs = shared_modules.Configs
local utils = shared_modules.Utils
local assets = ReplicatedStorage:WaitForChild("Assets")
local drops = ReplicatedStorage:WaitForChild("Drops")
local packages = ReplicatedStorage:WaitForChild("Packages")

local FormatNumber = require(utils.FormatNumberAlt)
local ZoneConfig = require(configs.Zone)
local DropConfig = require(configs.Drop)
local Knit = require(packages.Knit)

local DropController = Knit.CreateController {
	Name = "DropController"
}

local DROP_DEBRIS_FOLDER = Instance.new("Model")
DROP_DEBRIS_FOLDER.Name = "DropDebris"
DROP_DEBRIS_FOLDER.Parent = workspace

local drop_highlight = Instance.new("Highlight")
drop_highlight.DepthMode = Enum.HighlightDepthMode.Occluded
drop_highlight.FillTransparency = 1
drop_highlight.OutlineColor = Color3.fromRGB(0, 0, 0)
drop_highlight.Parent = DROP_DEBRIS_FOLDER

local LAST_PICK_UP = {
	["Coins"] = nil;
	["Gems"] = nil;
}
local CURRENCY_IMAGES = {
	["Coins"] = "rbxassetid://18560037692";
	["Gems"] = "rbxassetid://18345440521";
}

local function createPickupIndicator(position, addedValue, currencyType)
	if LAST_PICK_UP[currencyType] then
		task.cancel(LAST_PICK_UP[currencyType])
		LAST_PICK_UP[currencyType] = nil
	end
	
	local part = workspace:FindFirstChild("BillboardDropHolder"..currencyType) or Instance.new("Part")
	part.Anchored = true
	part.CanCollide = false
	part.Position = position
	part.Transparency = 1
	part.Size = Vector3.one
	part.Name = "BillboardDropHolder"..currencyType
	part.Parent = workspace
	
	if part:FindFirstChild("CurrencyPickupBillboard") then
		local billboard_gui = part["CurrencyPickupBillboard"]
		billboard_gui.StudsOffset = if currencyType == "Gems" then Vector3.new(0, 1, 1) else Vector3.zero
		billboard_gui.MainFrame.FrameHolder.CurrencyImage.CurrencyAmount.Text = FormatNumber.FormatCompact(tonumber(billboard_gui.MainFrame.FrameHolder.CurrencyImage.CurrencyAmount.Text) + addedValue, 2)
		TweenService:Create(billboard_gui, TweenInfo.new(0.5), {
			StudsOffset = billboard_gui.StudsOffset + Vector3.new(0, 0.3, 0)
		}):Play()
	else
		local billboard_gui = assets.BillboardGuis.CurrencyPickupBillboard:Clone()
		billboard_gui.StudsOffset = if currencyType == "Gems" then Vector3.new(0, 1, 1) else Vector3.zero
		billboard_gui.MainFrame.FrameHolder.CurrencyImage.Image = CURRENCY_IMAGES[currencyType]
		billboard_gui.MainFrame.FrameHolder.CurrencyImage.CurrencyAmount.Text = FormatNumber.FormatCompact(addedValue, 2)
		billboard_gui.Parent = part
		TweenService:Create(billboard_gui, TweenInfo.new(0.5), {
			StudsOffset = billboard_gui.StudsOffset + Vector3.new(0, 0.3, 0)
		}):Play()
	end
	
	LAST_PICK_UP[currencyType] = task.delay(2, function()
		local billboard = part:FindFirstChild("CurrencyPickupBillboard")
		if billboard then
			billboard.Name = "Destroying"
			Debris:AddItem(billboard, 1)
			for _, v in billboard:GetDescendants() do
				if v:IsA("ImageLabel") then
					TweenService:Create(v, TweenInfo.new(1), {
						ImageTransparency = 1;
					}):Play()
				elseif v:IsA("TextLabel") then
					TweenService:Create(v, TweenInfo.new(1), {
						TextTransparency = 1;
					}):Play()
				elseif v:IsA("UIStroke") then
					TweenService:Create(v, TweenInfo.new(1), {
						Transparency = 1;
					}):Play()
				end
			end
		end
	end)
end

function DropController:KnitStart()
	coroutine.wrap(function()
		--while true do
			
		--	self:Drop("Coin", Vector3.new(102.1, 7.005, -182), 10)
		--	self:Drop("Gem", Vector3.new(102.1, 7.005, -184), 10)
			
		--	task.wait(40)
		--end
	end)()
	RunService.Stepped:Connect(function()
		for _, v in DROP_DEBRIS_FOLDER:GetChildren() do
			if v:IsA("BasePart") then
				if not v:GetAttribute("IsReady") then continue end
				
				if client_player.Character and client_player.Character:FindFirstChild("HumanoidRootPart") then
					v.CFrame = v.CFrame:Lerp(client_player.Character.HumanoidRootPart.CFrame, TweenService:GetValue(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut))

					if client_player:DistanceFromCharacter(v.Position) <= 5 then
						local drop_cost = DropConfig.DefaultRewards[v:GetAttribute("DropType")][v:GetAttribute("DropName")]
						Knit.GetService("DropService").PickupDrop:Fire(drop_cost, v:GetAttribute("DropType"))
						createPickupIndicator(client_player.Character.Head.Position, math.round(drop_cost * ZoneConfig.ZoneMultipliers[client_player.PersonalStats.CurrentZone.Value]), v.Name)
						v:Destroy()
					end
				end
			end
		end
	end)
end

function DropController:Drop(dropType: "Coins" | "Gems", breakableName, position, rate)
	assert(drops:FindFirstChild(dropType) ~= nil, `DropType {dropType} is not valid!`)
	task.spawn(function()
		local sound_part = Instance.new("Part")
		sound_part.Anchored = true
		sound_part.Position = position
		sound_part.CanCollide = false
		sound_part.Transparency = 1
		sound_part.Size = Vector3.one
		sound_part.Parent = workspace

		local drop_sound = SoundService.CoinDrop:Clone()
		drop_sound.Parent = sound_part
		drop_sound:Play()
		Debris:AddItem(sound_part, drop_sound.TimeLength)

		for _=1, rate do
			local drop_instance: BasePart = drops[dropType]:Clone()
			drop_instance.Position = position
			drop_instance.Parent = DROP_DEBRIS_FOLDER
			drop_instance:SetAttribute("DropType", dropType)
			drop_instance:SetAttribute("DropName", breakableName)

			drop_instance.AssemblyLinearVelocity = Vector3.new(math.random(-15, 15), 50, math.random(-15, 15))
			task.delay(1, function()
				drop_instance:SetAttribute("IsReady", true)
			end)
		end
	end)
end

return DropController
2 Likes

Rookie mistake chat, invisible walls caused the problem it was not anchored at all LOL

1 Like

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