Hi!
I have a game called Brick Inc. where you collect bricks.
The scripts responsible for the collection of bricks causes a lot of lag. Its activity is at about 20%
and the rate/s rises by a few thousand per second.
Heres the script + the module in Server Script Service (Please help!)
local Folder = game:GetService("Workspace"):WaitForChild("Bricks")
local BrickCollection = require(game:GetService("ServerScriptService"):WaitForChild("BrickCollection"))
local NameTable = {"BrickA", "BrickB", "BrickC", "BrickD"}
Folder.ChildAdded:Connect(function(child: BasePart)
do
--print("ChildAddedToBrickFolder")
-- if table.find(NameTable, child.Name) == nil then return end
if not child:FindFirstChild("Player") then return end
if not child:FindFirstChild("Collected") then return end
if not child:FindFirstChild("Worth") then return end
if child:FindFirstChild("Collected").Value == true then return end
local player = child:FindFirstChild("Player").Value
local debounce
local connection = child.Touched:Connect(function(otherPart: BasePart)
if debounce then return end
--print("ChildFromBrickFolderTouched")
debounce = true
local TouchingModel = otherPart:FindFirstAncestorOfClass("Model")
if TouchingModel then
local PlayerValue = TouchingModel:FindFirstChild("Player")
if PlayerValue then
if PlayerValue.Value == player then
BrickCollection.collect(otherPart, player, child)
end
end
end
debounce = false
end)
child.Destroying:Once(function()
--print("ChildDestroyingFromBrickFolder(:Once)")
if connection.Connected == true then
connection:Disconnect()
end
connection = nil
end)
end
end)
Module:
local BrickCollection = {}
local multT = require(game:GetService("ServerScriptService").Upgrades).getUpgrade("BrickMultiplier")
local cdT = require(game:GetService("ServerScriptService").Upgrades).getUpgrade("SpawnBrickCooldown")
local ss = game:GetService("SoundService")
local PB = require(game:GetService("ServerScriptService"):WaitForChild("PlaceBricks"))
local PlaceBrick = PB.PlaceBrick
local random = math.random
local sfxEvent = game:GetService("ReplicatedStorage"):WaitForChild("ClientFXEvents"):WaitForChild("ClientSFX")
local MarketServ = game:GetService("MarketplaceService")
local BricksMultiplierPassID = 1019423456
local round = math.round
local function awardBricks(receiver: Player, brick: Part)
do
--print("AwardBricksStarted")
if not receiver then return end
if not brick.Parent then return end
if not brick.Parent:FindFirstChild(brick.Name) then return end
if not brick:FindFirstChild("Worth") then return end
if not brick:FindFirstChild("Player") then return end
if not brick:FindFirstChild("Collected") then return end
if brick:FindFirstChild("Collected").Value == true then return end
if not receiver:FindFirstChild("Quests") then return end
if not receiver:FindFirstChild("Quests"):FindFirstChild("GoatedBrick") then return end
if not receiver:FindFirstChild("Quests"):FindFirstChild("WeakBrick") then return end
if not receiver:FindFirstChild("SpawnBrickCooldown") then return end
if not receiver:FindFirstChild("OnCD") then return end
if not receiver:FindFirstChild("LockPurple") then return end
if not receiver:FindFirstChild("NextBrick") then return end
if not receiver:FindFirstChild("Multipliers"):FindFirstChild("TotalMultiplier") then return end
brick:FindFirstChild("Collected").Value = true
-- Quests --
local gbProgress
local gb = receiver:FindFirstChild("Quests"):FindFirstChild("GoatedBrick")
if brick.Name == "BrickD" and gb:FindFirstChild("GoatedBrickQuestCompletion").Value == false and gb:FindFirstChild("GoatedBrickStarted").Value == true then gbProgress = gb:FindFirstChild("GoatedBrickStarted"):FindFirstChild("Progress") end
if gbProgress ~= nil then gbProgress.Value += 1 end
local wbProgress
local wb = receiver:FindFirstChild("Quests"):FindFirstChild("WeakBrick")
if brick.Name == "BrickA" and wb:FindFirstChild("WeakBrickQuestCompletion").Value == false and wb:FindFirstChild("WeakBrickQuestStarted").Value == true then wbProgress = wb:FindFirstChild("WeakBrickQuestStarted"):FindFirstChild("Progress") end
if wbProgress ~= nil then wbProgress.Value += 1 end
-- --
-- Multipliers + Award Bricks --
local brickS: NumberValue = receiver:FindFirstChild("leaderstats"):FindFirstChild("Bricks")
local TotalMultiplier = receiver:FindFirstChild("Multipliers"):FindFirstChild("TotalMultiplier").Value
brickS.Value += round(brick:FindFirstChild("Worth").Value * TotalMultiplier)
local cb = receiver:FindFirstChild("CurrentBricks")
cb.Value -= 1
-- --
local soundEvent: RemoteEvent = sfxEvent:FireClient(receiver, ss[brick.Name], brick)
brick:Destroy()
local cd = cdT[receiver:FindFirstChild("SpawnBrickCooldown").Value][1]
-- Fast Spawn at max BrickSpawnCooldown --
if cd == 0 then
local pick = random(1, 2)
if pick == 1 then
local onCD = receiver:FindFirstChild("OnCD") -- BoolValue, parented to the player, that checks if the spawning is on cooldown
local brick = receiver:FindFirstChild("NextBrick").Value -- picks a random bricktype using the modulescript
if receiver:FindFirstChild("LockPurple").Value > 0 then brick = "BrickD" end
PlaceBrick(receiver, brick) -- makes another script actually place the brick
onCD.Value = false
end
end
end
end
function BrickCollection.collect(otherPart: BasePart, player: Player, brick: Part)
-- print("CollectBrickFired")
local pv
if otherPart:FindFirstAncestorOfClass("Model"):FindFirstChild("Player") ~= nil then
if otherPart:FindFirstAncestorOfClass("Model"):FindFirstChild("Player").Value == player then
pv = otherPart:FindFirstAncestorOfClass("Model"):FindFirstChild("Player").Value
end
end
if pv ~= nil then awardBricks(pv, brick) return end
if otherPart:FindFirstAncestorOfClass("Model") == player.Character then awardBricks(player, brick) return end
end
return BrickCollection
Thanks for any help! (also massive thanks to @BadDad2004 who already helped alot)