Recently I’ve made a plot saving system for a game I’m working on. An issue with it is that I don’t want to save player characters or anything that has a different name then “Part”. Since I’m using F3X all parts created will have that name.
This is the function to remove anything else that’s not named “Part”
for i,v in pairs(partsToSave) do
if v ~= nil and v.Name ~= "Part" and v.Name ~= "Mesh" then
local target = table.find(partsToSave, v)
table.remove(partsToSave, target)
end
end
So far, so good.
But once the saving part comes by, it deletes every single part in that array after saving, but it ends up deleting player characters and other things that aren’t named “Part” even though I specified that anything not named “Part” has to be removed.
This appear to happen randomly, as some character parts don’t get deleted and others do.
The following won’t 100% fix the issue, but it’s closer than what you have, read the rest of the post to get an idea of what to do:
if (v.Name == "Part") or (v.Name == "Mesh") then -- NAMED "PART" or "MESH"
The condition you set up and the described outcome are the exact same: Remove any part not named part or mesh, you say it removes things that aren’t named part (and I assume mesh).
I feel like there is missing information here, you should print the parts as they delete (or the contents before the operation begins) so you know that’s going on. It would make more sense that the player is not even in partsToSave to begin with, so maybe just deal with that if it happens to be the case. You could also hack in a solution with Players:GetPlayerFromCharacter or some derivative of that but I think it’s better to just fix the actual issue.
You’re also dealing with some dangerous code there, as you’re destroying an array you’re operating over, you may miss entries if you remove parts of it. Also, don’t have to find the target, it should be the same as i.
v can never be nil, pairs does not operate with nil values (if you change your loop method, that may not be true).
Tl;dr: Your condition is doing it’s job, the character will get deleted if it’s in there; just don’t put there character there in the first place? That or just use some method to flag/remove parts that are actually part of a player (not by name).
I’ve tried to do that solution but it appears that sometimes the “if” statement doesn’t really go through, for example, the HumanoidRootPart is not removed from the array, even though it’s name is not “Part” or “Mesh”.
In order to detect the parts that are inside of a plot, I use workspace:GetPartsInRegion3() and sometimes it adds “nil” to the array, this is why I added that check in the first place;
Yes, this is exactly what the function is supposed to do, remove the character itself and other parts from the array which were caught in the :GetPartsInRegion3() that aren’t meant to be saved.
partsToSave is a table containing instances found in a :GetPartsInRegion3()
The point of the i,v loop is to remove anything from the table that is not named “Part” or “Mesh”, because that’s how F3X parts are named when they’re created and they never changed. I don’t want to save anything else.
Problem is, some parts get removed from the table and others don’t, even if they meet the conditions to be removed, such as the “HumanoidRootPart”.
Same issue, as you can see it’s meant to remove parts from the array that aren’t named “Part” or “Mesh” but it didn’t remove some parts of my character which then caused it to be destroyed.
I see no issue at your end, did you check the part deletion code? I would say that you made something like local partsToDelete = partsToSave before running that loop then delete everything in partsToDelete making partsToSave useless but tables get passed by reference so what you do in partstosave also happens in partstodelete
Edit: you might also want to try adding print(v.Name) when you delete something from the table so you see what happens in background.
I’ve tried that tactic before, printing while it loops. It prints the “HumanoidRootPart” for example, but it doesn’t get removed from the Array, even though its name is not “Part” or “Mesh”
Alright, I’ve fixed the issue, I’ll make sure to document it here because some people may also stumble across it.
When using a loop, do not remove instances from the table you’re looping, otherwise, some instances will be ignored.
For example, the current index is 3 and the Instance[3] needs to be removed because it doesn’t meet the requirements, if you remove Instance[3], Instance[4] will become Instance[3] but it will be ignored since the loop will go to the next index, which is 4, so regardless if the previous Instance[4] also had to be removed, it won’t, because it is now Instance[3] and according to the loop he already went over that index.