#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)
There is a lot to this that causes this issue
Before I help what do YOU think is the main issue
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.
Learned that early on.
What else do you think could be causing the issue?
Uhh im gonna guess the run.Stepped thing maybe
Since thats also running like every frame
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
You know what pikmin is, right?
the nintendo game w the little colorful plant guys
Im basically making that but in roblox
So is it a folder containing a whole bunch of those dudes?
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
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
oh no
Just use a module
That is what they're used for
Is there a reason you use string.match instead of comparing two strings?
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
The module thing
Basically, the cursor the player has, when it expands that explanding sphere is called the calldot. Bcuz its not a local script, i just check for if a brickmin part touches any part named "calldot" to make something happen
Why do you use descendant rather than GetChildren()? It would iterate less
Ok there is a simple fix that is less complicated than your current implementation
i didnt know abt this. I did end up running into issues when initially trying to use game.Workspace.ChildAdded:Connect(function() back with the module script, as it wasnt finding the needed parts, then i just looked it up and saw i needed to use :GetDescendants, only for that to also end up not working
https://create.roblox.com/docs/reference/engine/classes/CollectionService
This is a service that allows developers to easily manage group of items with a specific tag name.
Now there is a WAY better way to do what you're doing
Im very familiar with this
Since all you want to do is get all Pikmin's within the sphere?
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
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
ah, ok
An oversight I found was you also put the OnServerEvent inside the RunSevice loop, which will also cause another MemoryLeak
Same with bricksInSpawn_RE
wait why does that make the memory leak
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
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
That is pretty common, tho I don't really know how you handle npcs and allat so I can't say much
as to fix this, just use what I said to fix it
Check if the sphere touched the pikmin
There is basically nothing yet, it was just yesterday i had gotten to working on the npc ai haha