Please help me optimize this script

ah yeah add Value.Changed thing forgot sorry

3 Likes

ahh nice thank you!
asd asd I hate char limit

3 Likes

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

3 Likes

Thanks for the awesome suggestion, it isnā€™t quite cut out for my game yet, but Iā€™ll adjust it

3 Likes

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

2 Likes

Thanks! very useful additional information :slight_smile:

1 Like

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

2 Likes

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

How we reduced bandwidth usage by 60x in Astro Force (Roblox RTS) - Resources / Community Resources - Developer Forum | Roblox

Network Optimization best practices - how to keep your gameā€™s ping low! - Resources / Community Tutorials - Developer Forum | Roblox

Real world building and scripting optimization for Roblox - Resources / Roblox Staff - Developer Forum | Roblox

1 Like