Like I somewhat explained above, you move around on a plate where all of these bricks are (and the CFrameValues’ Value is also there)
There is a hitbox as well parented to a players character and their collector npcs character as well
Hmmm alr I kind of see the thing. I would do something like this:
for i,v in pairs(folder for the brick or idk anything) do
v.Touched:Connect(function)
end
and for the npc can you tell is the npc going around randomly to find them or directly going to the positions to collect the parts?
Well, the Bricks are CFrame values and therefore cannot be touched
I believe that ridding the script of atleast one for-loop should lessen the activity rate by a whole lot. Can you help me achieve that?
then why there are hitboxes I dont understand the point. I either understand it wrong or there is a point ur missing.
Yes sure 1 minute and I will try to see if the script I will make will fit ur problem
Thank you so much (maxcharsmaxchars)
This is also badly optimised.
At best:
for _, v in ipairs(t) do
end
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)
maybe this idk Im in urry rn so it may be worng
what is the difference tho I always see that “_” but idk what it meant
_ 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.
This doesnt work as it only fires once when the script loads or a hitbox gets added
ah yeah add Value.Changed thing forgot sorry
ahh nice thank you!
asd asd I hate char limit
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)
now there are ways to make a more efficient version of this like using spatial hashes where the map is divided into grids its explained better in this topic in the coins collection part
Real world building and scripting optimization for Roblox - Resources / Roblox Staff - Developer Forum | Roblox
Thanks for the awesome suggestion, it isn’t quite cut out for my game yet, but I’ll adjust it
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
Thanks! very useful additional information
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
np glad it works
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
these topics map help