#Optimization issue
1 messages · Page 1 of 1 (latest)
** You are now Level 3! **
this is such bad code 😮💨
ye ik but idk what can I do with it
what can I do with this code rn? I just dont know
i tried all ways to optimize
like I tried to make connections and after it disconnect them when disaster ends
and still doesnt work tho
what are these values ```lua
local strikes = math.random(Module_S:Get("ThunderStorm").MinThunders, Module_S:Get("ThunderStorm").MaxThunders)
for i = 1, strikes do
task.wait(randomRange(Module_S:Get("ThunderStorm").MinInterval, Module_S:Get("ThunderStorm").MaxInterval))
local Count = math.random(Module_S:Get("ThunderStorm").MinThundersPerStrike, Module_S:Get("ThunderStorm").MaxThundersPerStrike) ```
local clone = ReplicatedStorage.Models.Disasters.ThunderStorm:FindFirstChild("ThunderPart") make sure this is anchored
ye its anchored
thats the part that have some customization that i dont want to set by the script
so i just copy that part
that values are the stats of the thunderstorm
that module script keeps all info about disasters
by function Get we use new stats of thunderstorm
yes, what are the specific values you are using?
because i have some upgrades that help player to survive from disasters
as in what is the min and max you have in there
min and max means the max and minimum count of thunders like Min is 65 thunders and max 105
i am aware what min and max means, just tell me the actual numbers you're using
like, there - great - you have min/max of 65 to 105 Thunders. what are the others?
here u are
ThunderStorm = {
MinThunders = 65,
MaxThunders = 105,
MinInterval = 0.3,
MaxInterval = 0.7,
MinThundersPerStrike = 1,
MaxThundersPerStrike = 3,
},
you are attempting to create approximately 2000+ instances in a single frame (on average). of course it's going to lag.
and it is very likely each one of them is triggering touch with its neighbors and requesting local plr = game.Players:GetPlayerFromCharacter(char) if not plr then return end about 4,000 times in one frame
not too sure on that one
depends when .touched actually 'activates' for these parts
so the only way to fix this just decrease parts count?
lag usually comes from iterating too many things at once
it's not an "optimization" thing
it's just, you're doing too much at once
tbh most of the time, optimizations have negligible impact on your games actual performance
if there's ever any lag, it's going to be from iterating too many things at once
or calling expensive functions like game:GetDescendants() 100 times in a single frame
everything else is microoptimizations that barely even matter
look when the disaster doesnt spawn i have like 180 fps but when it spawns fps is falling down and after it dissapears fps comes back
can also depend what's actually being cloned in here lua local clone = ReplicatedStorage.Models.Disasters.ThunderStorm:FindFirstChild("ThunderPart") local p = clone:Clone()
what?
i read your first post bro
I mean when the disaster doesn’t spawn, I have around 180 FPS, but when it spawns, the FPS drops. After it disappears, the FPS goes back to normal.
yes that's what you wrote in the first post
no
i mean they might but i'm pointing out that this isn't an optimization issue
this is, you made a mistake and/or doing too much at once issue
e.g if there's an error in there, that's going to cause lag too for each of the 2000 instances you're creating
because print()/error() etc is laggy/slow
maybe you didn't realize you were creating and working with that many instances at once
oh got it
Should i just make like 1 hitbox for each thunder and make the visual by beam?
that doesn't fix 2000 instances in a single frame
also
in case you're wondering where i got the 2000 number from, the average starting height is 650, the average height of your beam parts is 30, and 650/30 is approximately 20.
then i just took 100 for the thunders
20*100 = 2000
and i didn't see any yielding between the creation of all 2000
Actually I tested something — when I changed the lightning so each strike is just 1 straight part instead of multiple bent segments, the lag basically disappeared.
So it looks like the issue is directly related to the number of instances being created, not the effect itself.
The moment I go back to segmented lightning, the FPS drops again.
yes exactly.
why else do you think i bee-lined for this number?
... because I know in advance this is where 80% of lag comes from
this part just got a sound nothing else and some attributes that works with other function like breaking generator and so on
that helps but you're still creating 2000 copies and iterating over all 2000 a couple of times
2000 is a lot of anything
like even if you just do
for i=1,2000 do Instance.new("Part",workspace):Destroy() end you gonna get lag
and this is what you are doing
OH u mean the most lag is the destroying not the spawn?
** You are now Level 4! **
oh wait
doing anything 2000 times is going to be slow
particularly if it is every frame
my suggestion is to just make it so you dont spawn 100 lighting strikes at once
spread them out over time, like one every 3 frames instead of 100 in one frame
that's normally how lightning works after all. you don't ever see 100 strikes at the exact same moment
Got it. So the issue is the burst of ~2000 instances in one frame.
heck it's probably even more than that
because I didn't count this ```lua
local Count = math.random(Module_S:Get("ThunderStorm").MinThundersPerStrike, Module_S:Get("ThunderStorm").MaxThundersPerStrike)
for j = 1, Count do```
which is on average 2 of them
so, ~4000 instances
OHHH mann i got it thankss I just need to lower instances like not creating that much strikes
you also creating alot of FolderOfThunderStorms, and making alot of connections which will cause memory leaks, but you should focus on fixing spawning large number of instance each frame first
so for signle thunder thats 2000 but i need to make ot more lower
ok then ill make that connections gonna disconnect after finishing disaster
local strikes = math.random(Module_S:Get("ThunderStorm").MinThunders, Module_S:Get("ThunderStorm").MaxThunders)
for i = 1, strikes do
task.wait(randomRange(Module_S:Get("ThunderStorm").MinInterval, Module_S:Get("ThunderStorm").MaxInterval))
local Count = math.random(Module_S:Get("ThunderStorm").MinThundersPerStrike, Module_S:Get("ThunderStorm").MaxThundersPerStrike)
for j = 1, Count do
task.spawn(function()
...
thunder(targetPos)
end)
end
end
``` there's a yield inside thunder(), but you wrapped the call in task.spawn so that yield doesn't mean anything; thunder() is called, what, 200 times in a single frame?
you should always be very mindful when you're creating large loops like the one that creates 20 instances per lightning bolt, and particularly nested loops where you have for x=1,n do for y=1,n do ... end end
because the number of iterations increases very fast
its better if you manually create a folder in workspace, and create that connection once, inside your script instead of each time the event is fired
that is prone to error, yes
but not how it currently is since all lightning gets created in the same frame
add in the expected yield, and that folder occasionally becomes empty during the event both breaking the event early and spamming errors due to all the threads that are expecting that folder to exist
but eh, i'll let you explain the rest. god speed 
ye its a huge mess
you're telling me
its better if he just focuses on the 2000 instances though then worry about memory leakage cause by the 200 folder that'll be spawned in
local FolderOfThunderStorm = Instance.new("Folder")
FolderOfThunderStorm.Name = "ThunderStorm"
FolderOfThunderStorm.Parent = workspace
LightningFolder = FolderOfThunderStorm
FolderOfThunderStorm.ChildRemoved:Connect(function(child)
local count = FolderOfThunderStorm:GetChildren()
if #count <= 0 then
FolderOfThunderStorm:Destroy()
disasterstarted.Value = false
if clonesky then
clonesky:Destroy()
local Atmosphere = game:GetService("Lighting"):FindFirstChildOfClass("Atmosphere")
if Atmosphere then
AtmosphereController:Remove("ThunderStorm", 0.2)
end
end
end
end)
not quite a memory leak
the folder:destroy is in there
yeah, no, the problem is if that folder becomes empty during the event, then any pending threads will throw error
coz they expect the folder
but that's only if you yield between each call to thunder()
so the most important one is just 2000 instances in 1 frame
and i need to make it lower and not in 1 frame right?
in one frame, and 2000 instances at once
because they're all connected to .touch and i suspect that's firing for between segments of the lightning
not certain of that but if you add a print in there to see when .touched is firing it should be very easy to tell when it outputs 4000+ times or does not
but when i was testing the code after removing touch the code was still lagging so this touches doesnt make that much lags like creating 2k istances in 1 frame
but yeah main thing is 2k instances in one frame, yes
trying to say that's not the only thing. you'll figure it out 😂
still worth checking tho
ye ig xd
it's not the primary cause, but it isn't helping either if that theory is true
k ill check by prints
welcome to scripting: every theory you have needs to be tested
give me 1 minute ill be back and say result how many prints do i have
yeah
also man
how long are u scripting?
too long
great so thats probably not a problem
is this a server script?
i'm guessing its a server script
so, few things there, all of those instances need to be networked. that's going to be expensive
this is probably way outside your capabilities but to do this optimally, you would do most of the work of creating instances and detecting hits on the client
since the client is responsible for firing .touched anyway
if you wanted it synced between all players and maybe add some server authority, you would use deterministic random and mathematically determine hits on the server in addition to the client's rendering and checking if themselves were hit
one of the tricks being that you do a spatial query like :FindPartsInBox() around each player, rather than spatially querying or connecting to .touched on every lightning segment
since you only care about players. if you wanted environmental damage, you'd still probably use a very primitive set of raycasts, rather than raycasting every segment
depends how far you want to take it
not only players
oh wiat
wait
i got it
i just didnt understand correctly before
oh man after removing task.spawn and adding a task.wait that rlly helped but not fully
also I dont understand why when the thunderstorm spawns its ok no lags but after like 4-5 times thunderstorm starts freezing and lagging ye ig its again istances but why does it doesnt lag on first time
well keep going with it 👍 when you figure this out you won't have to solve it again
and someday, someone might have the same question you do, and you will know the answer
and not just for the sake of the answer, but how to reach the answer too
it's the difference between knowing 2+2=4 and knowing how to add 2 with 2 to get 4.