#Optimization issue

1 messages · Page 1 of 1 (latest)

wooden quail
#

When ThunderStorm disaster starts thats still ok but after like 3-4 times after thunderstorm was spawned the new spawn of thunderstorm is just killing my pc like before spawn i had 180 fps and after it beigns like 30-40 or even lower

void pivotBOT
#

studio** You are now Level 3! **studio

surreal kestrel
#

this is such bad code 😮‍💨

wooden quail
#

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

surreal kestrel
#

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

wooden quail
#

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

wooden quail
#

that module script keeps all info about disasters

#

by function Get we use new stats of thunderstorm

surreal kestrel
wooden quail
#

because i have some upgrades that help player to survive from disasters

surreal kestrel
#

as in what is the min and max you have in there

wooden quail
#

min and max means the max and minimum count of thunders like Min is 65 thunders and max 105

surreal kestrel
#

like, there - great - you have min/max of 65 to 105 Thunders. what are the others?

wooden quail
#

here u are

ThunderStorm = {
    MinThunders = 65,
    MaxThunders = 105,
    MinInterval = 0.3,
    MaxInterval = 0.7,
    MinThundersPerStrike = 1,
    MaxThundersPerStrike = 3,
},
surreal kestrel
wooden quail
#

ohh

#

i was just trying to make it more like as a real thunder

surreal kestrel
#

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

wooden quail
#

so the only way to fix this just decrease parts count?

surreal kestrel
#

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

wooden quail
#

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

surreal kestrel
#

can also depend what's actually being cloned in here lua local clone = ReplicatedStorage.Models.Disasters.ThunderStorm:FindFirstChild("ThunderPart") local p = clone:Clone()

wooden quail
#

so other functions doesnt take much fps like this one

#

thats just a part

wooden quail
#

not mesh not anything else

#

just part

surreal kestrel
#

i read your first post bro

wooden quail
# surreal kestrel what?

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.

surreal kestrel
wooden quail
#

oh

#

i just thoguht

#

thought*

#

u mean that maybe other script takes fps

surreal kestrel
#

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

wooden quail
#

oh got it

#

Should i just make like 1 hitbox for each thunder and make the visual by beam?

surreal kestrel
#

that doesn't fix 2000 instances in a single frame

wooden quail
#

also

surreal kestrel
surreal kestrel
#

20*100 = 2000

surreal kestrel
wooden quail
#

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.

surreal kestrel
surreal kestrel
wooden quail
#

this part just got a sound nothing else and some attributes that works with other function like breaking generator and so on

surreal kestrel
#

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

wooden quail
#

OH u mean the most lag is the destroying not the spawn?

void pivotBOT
#

studio** You are now Level 4! **studio

surreal kestrel
#

what

#

bruh

wooden quail
#

oh wait

surreal kestrel
#

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

wooden quail
#

Got it. So the issue is the burst of ~2000 instances in one frame.

surreal kestrel
#

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```
surreal kestrel
#

so, ~4000 instances

wooden quail
#

OHHH mann i got it thankss I just need to lower instances like not creating that much strikes

slow elk
wooden quail
#

so for signle thunder thats 2000 but i need to make ot more lower

wooden quail
surreal kestrel
#
        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

slow elk
surreal kestrel
#

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 salute

surreal kestrel
slow elk
#

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

surreal kestrel
# slow elk its better if he just focuses on the 2000 instances though then worry about memo...
        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

slow elk
#

oh

#

I didn't see that

#

mb

surreal kestrel
#

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()

wooden quail
#

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?

surreal kestrel
#

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

wooden quail
#

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

surreal kestrel
#

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 😂

surreal kestrel
#

it's not the primary cause, but it isn't helping either if that theory is true

wooden quail
#

k ill check by prints

surreal kestrel
#

welcome to scripting: every theory you have needs to be tested

wooden quail
#

give me 1 minute ill be back and say result how many prints do i have

wooden quail
#

also man

#

how long are u scripting?

surreal kestrel
wooden quail
#

cool

#

i added print

#

but

#

there is no prints on console

surreal kestrel
#

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

wooden quail
#

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

surreal kestrel
#

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.

wooden quail
#

got it

#

thanks man