local ws = game:GetService("Workspace")
local rs = game:GetService("ReplicatedStorage")
local BrickCollection = require(game:GetService("ServerScriptService"):WaitForChild("BrickCollection"))
local bricksFolder = ws:WaitForChild("Bricks") -- this ur bricks folder
ws.DescendantAdded:Connect(function(descendant: Instance)
if not descendant:IsA("BasePart") or descendant.Name ~= "ExtendedHitbox" then return end
local ancestorModel = descendant:FindFirstAncestorOfClass("Model")
if not ancestorModel then return end
local descendantPlayer = ancestorModel:FindFirstChild("Player")
if not descendantPlayer or not descendantPlayer.Value then return end
for _, brick in pairs(bricksFolder:GetChildren()) do
local brickPlayer = brick:FindFirstChild("Player")
local brickValue = brick:FindFirstChild("Value")
if brickPlayer and brickPlayer.Value == descendantPlayer.Value and brickValue then
local partsInBounds = ws:GetPartBoundsInBox(brickValue.Value, Vector3.new(1,1,1))
for _, part in pairs(partsInBounds) do
if part ~= descendant then return end
BrickCollection.collect(descendant, descendantPlayer.Value, brick)
break
end
end
end
end)
_ represents an unnecessary variable. By replacing i with _, it shows that i is unneeded and will then be discarded from memory and ultimately optimising the loop.
1 ws.DescendantAdded
since you want that for only the bricks you can add them into a folder and do ws.BricksFOlder.ChildAdded
2
you don’t have to use pairs since instances keys are numbers
do ws:FindFirstChild("Bricks"):GetChildren()
instead of pairs(ws:FindFirstChild("Bricks"):GetChildren())
3
the script seems complicated i am guessing that you are trying to detect when a brick is collected like the Roblox incremental games if though you can do a magnitude check to check the distance
this isn’t the most efficient solution
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local bricks = workspace.Bricks -- folder to hold bricks
local character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()
local humanoidRootPart :Part = character:WaitForChild("HumanoidRootPart")
local collectionRange = 15 -- 15 studs
RunService.Heartbeat:Connect(function()
for _, brick :Part in bricks:GetChildren() do
if (brick.Position - humanoidRootPart.Position) > collectionRange then
-- the brick is collected
brick:Destroy()
end
end
end)
For everyone looking to get that extra 0.0001 fps: Using _ does NOT remove it from memory, all it does is just signify that you wont use it, the script will still update the _ as any other letter so it doesn’t matter what you use it only improves readability
Well, I somehow managed to fix the lag using a method similar to GE_0E’s, even if barely. Now im facing another problem (which is old):
When playing the game, the network RECV is extraordinarily high (90-300kb/s). Can anybody inform me of the does and do-nots when trying to keep the network rcv low?
Edit Note: I realised it rises when a lot of unions are replicated from the replicated storage. Is there any way to reduce this lag without removing the unions? thanks!
Edit 2: Another large problem: I beat the super-high script rate/s, but now the activity doesnt stop rising
high script rates is because your script is running each heartbeat/frame that isnot a problem at all
if it keeps rising without going down then make sure that you haven’t made any memory leaks
the RECV will increase whenever you replicate/send information from server to client
1- try to not send as much data through remove events
2- you can try doing optimization techniques to reduce the sent data (there are alot of topics for this)
3-any instance that gets added to replicatedStorage or workspace will increase the network recv
4-changing the properties of instances in workspace/replicated storage through server will increase the network recv
if the unions aren’t replicated often then its fine
aka replicating a map from server storage to workspace/replicated storage that will increase the Recv but only for short time