#This code is giving me frame drops but I'm not sure of how else I can do this.

1 messages · Page 1 of 1 (latest)

gaunt crown
#

I'm probably just too tired to think straight and maybe I'll figure out the solution in the morning, but as of now, I can't seem to think of any other way to do this.

#

This is pretty much the whole thing

carmine thicket
#

Before I help what do YOU think is the main issue

gaunt crown
#

Initially I thought it was just the printing that was constantly happening that was causing frame drops, but the frame drops persist even when commented out.

carmine thicket
#

print can also cause lag

#

Since they are surprisingly expensive

gaunt crown
#

Learned that early on.

carmine thicket
gaunt crown
#

Uhh im gonna guess the run.Stepped thing maybe

#

Since thats also running like every frame

carmine thicket
#

Alright lets learn!

#

So furst Runservice runs the code every frame

#

Each event apart of that service runs at different times during the frame process

#

Code HAS to execute before Roblox actually renders each frame

#

In your case, you run a for loop and check for each npc in your game?

#

What is Brickmin actually

#

@gaunt crown

gaunt crown
#

the nintendo game w the little colorful plant guys

carmine thicket
#

I heard of it

#

Oh yeah

gaunt crown
#

Im basically making that but in roblox

carmine thicket
#

So is it a folder containing a whole bunch of those dudes?

gaunt crown
#

Yes, every single brickmin that is spawned goes into _G["Brickmin"]

#

I read that its a global table

#

So i can access it within all scripts in serverscriprservice

carmine thicket
#

Well one issue is that you iterate over EVERYTHING in each character. Afterwords, you also create a .Touched connection. This isn't inheritenly bad but each time this code runs, it creates a new .Touched event which will cause a memory leak

gaunt crown
#

oh no

carmine thicket
#

That is what they're used for

carmine thicket
gaunt crown
#

I did try that earlier, actually. I would get all workspace descendants each time a descendant got added, and if the desc had the tag of "brickmin" and was a model, it would be added to a table. However, it would run way more than i wanted it to, so i just went back to trying with the global table

gaunt crown
carmine thicket
carmine thicket
gaunt crown
carmine thicket
#

Now there is a WAY better way to do what you're doing

carmine thicket
#

Since all you want to do is get all Pikmin's within the sphere?

gaunt crown
#

Yes, basically. My initial idea was if a calldot touches a brickmin part, the parts parent, which would be a model, would then look for the hrp within itself, and then move toward the player that the calldot that touched it belonged to. As of now im just making sure the script will be able to print the calldot parent and the parent of the brickmin part that it touched, which it can at the cost of some frames

carmine thicket
#

Ok for the new fix, instead of checking if the brikman touched the ball, we would check if the ball touched the brikman. To do this, we can use a querying method in the workspace called :GetPartsBoundInRadius

#

And you would want to create a new table outside of the RunService loop that checks if the Brikman was already found/touched with the ball

gaunt crown
#

ah, ok

carmine thicket
#

Same with bricksInSpawn_RE

gaunt crown
#

wait why does that make the memory leak

carmine thicket
#

When you use :Connect on a function, it basically wraps the function you gave to that connection to that event when that event fires

#

Each time a event fires, it tries to call/activate the function for each connection

#

Here is an example:

local MyEvent = Instance.new("BindableEvent")

for i = 1, 4 do
    MyEvent.Event:Connect(function()
       --This function will activate when :Fire() has been called
        print("The event has fired")
    end)
end

while true do
    task.wait(1)
    MyEvent:Fire() --When this fires,  it will try to activate ALL connected functions
end
#

In this example, I use a for loop to connect an event 4 times

#

This means when MyEvent fires, the game will print the stuff within the Event 4 times

gaunt crown
#

ohh

#

ok yeah that adds up

#

but like

#

Im not sure how else to keep the thing updated aside from using runservice because brickmin arent initially present in the workspace

#

For instance, printing _G["Brickmin"] would result in nothing since it checks as soon as the game starts and there are no brickmin present at the start

carmine thicket
#

That is pretty common, tho I don't really know how you handle npcs and allat so I can't say much

carmine thicket
#

Check if the sphere touched the pikmin

gaunt crown