If name contains certain word (like string.find but names)

How is this suggestion any different from mine?

Sorry I dont read the comments


doesn’t seem to be working?? I regen the plane but the model does not get deleted…

You need to for loop through the Vehiclesinspawn since it’s a table and does not have a .Name property. Once you for loop through the table you take the value (instance) and do value.Name and match the string on that Name.

for _, child in ipairs(game.Workspace:GetChildren()) do
   local childUID = string.match(child.Name, "(.*)redcamaro")
   if childUID then
      -- Do stuff here.
      print(childUID)
   end
end
for x, Vehiclesinspawn in pairs(x) do

I wanted to point out that the above is spaghetti code. If you happened to use x in the code within the for loop at some point, it will refer to the integer incrementing, not to the actual x variable you set to script.Parent. You’d probably want to switch the for x to something like for i, just to be safe (or just not use the variable x).

Also, this should fix your problem with this:

local Vehiclesinspawn = script.Parent:GetChildren()
for i, v in pairs(Vehiclesinspawn) do
    if string.match(v.Name, "BlueFighter") then
        for x, Vehiclesinspawn in pairs(script.Parent) do
            if (Vehiclesinspawn.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude <= Range then
                Vehiclesinspawn:Destroy()
            end
        end
    end
end

I recommend not teaching the use of pairs on a table that is not a dictionary, refer to this thread & post:

Hmm okay, interesting. I’m aware of the use of ipairs as well for maintaining the order of an array with key indexes. The thing is, with this particular problem, maintaining order when linearly searching through the table doesn’t actually matter.

With that being said, here’s the same method using a numerical for loop:

local Vehiclesinspawn = script.Parent:GetChildren()
for i = 1, #Vehiclesinspawn, 1 do
    if string.match(Vehiclesinspawn[i].Name, "BlueFighter") then
        for x, Vehiclesinspawn in pairs(script.Parent) do
            if (Vehiclesinspawn.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude <= Range then
                Vehiclesinspawn:Destroy()
            end
        end
    end
end

It matters considering he was talking about having up to 10000 models. That’s why I recommend ipairs since it’s the fastest and because it’s according best practice standards for this purpose.

Take a look at this thread for speed comparisons:

Cant you just instead of calling it random…carname just call it like playerusername…carname

Thanks for the reply. However, he never talked about having up to 10000 models, but rather, he stated the following:

Besides, the maximum number of players in a Roblox game is 100. And assuming anywhere from a 1:1 to 10:1 car to player ratio, there likely would not be more than 100 - 1000 car models in a game at any given point; and likely that would be stretching optimal performance by having that many car models on a single game server.

That’s an informative post you sent; thanks for the link. The thing is, when he benchmarks the results he has to use this: sum = sum +(((tick()-t)*10^6 )). He’s taking the miliseconds it takes and scales it 10 times itself to the 6th exponent. Let’s take the “slowest” loop, the numerical loop, as an example.

Numerical for Loop

The results from the numerical loop were 68744.8ms. To reverse the effects of *10^6, we can do this:
68744.8 / 10 = 6874.48ms
And log6 of 6874.48 = 4.93122622ms

So, the numerical for loop took approximately 4.93122622ms to complete, or 0.00493122622 seconds. Now let’s see how the ipairs loop did:

ipairs for Loop

The result from the ipairs loop was 55565.7ms. Let’s again reverse the effects of *10^6:
55565.7 / 10 = 5556.57ms
log6 of 5556.57 = 4.81244075ms

So it took 4.81244075ms, or 0.00481244075 seconds to complete.

The Difference

Using the over the top situation where OP has 10000 models to search through, according to the results in the topic you sent, here’s about how long it’d take for the numerical for loop and the ipairs for loop:

Numerical: 0.00493122622s * 10000 = 49.3122622s
ipairs = 0.00481244075s * 10000 = 48.1244075s

The difference between the numerical for loop and the ipairs for loop would come out to be merely 1.1878547 seconds for ten thousand elements to sort through. Considering the even less noticeable difference between ipairs and pairs in the topic you sent me, it’d likely be around a 0.5s - 0.8s difference.

On top of that, the benchmarks were performed on an array of strings; this benchmark does not account for differences in behavior in the Roblox workspace, or in general other scenarios. In this article, @sleitnick establishes that the difference in performance between a numerical for loop and an ipairs in a situation where ipairs is technically faster (read on the article) is merely ~2%.

He also says this clearly: “Do not use one type of loop over another for the sake of optimization, because they are all comparable in speed. The differences are too minuscule.”

Conclusion

With all that being said, I don’t believe optimization should be a valid reason to prefer one loop over another, unless you specifically benchmark it in your own project and find one to be preferable in that situation; there is no singular “faster” loop in my own opinion, which is based on the facts. Choosing which looping method to use really just comes down to personal preference and what works best for a particular situation; there is no “best standard practice” that has been firmly established.

2 Likes

jesus christ I leave my computer for like an hour and I come back to this guy painstakingly calculating how long it will take for my vehicle to spawn

thanks i guess

edit: i dont plan to have 10000 vehicles i just was giving the vehicle a serial, the more the digits, the less probability a model with the same serial will be regenerated

1 Like

Thanks for taking the time to write this. These are all good and valid points and will take this into consideration :+1:
You’re right, I misunderstood what he meant. Re-reading the articles I understand now that optimizing the code would not be ideal even if there were 10000 models. I personally usually tend to stick to the more faster methods even if they aren’t necessarily better.

However, by best practice I meant the following, and my apologies for not being clear about this:
Since GetChildren() finds the child instances in the model, there is no need to use pairs since pairs is used in situations where you want to iterate through associative tables, meaning finding Key & Value pairs. The key has a value. Key = Value. Does GetChildren() return keys? Pairs can return both types, the key or the index if there is no key, but in this context it only returns indexes.

‘ipairs’ on the other hand returns the current Index of the table being iterated through and what value is on that index. Since GetChildren() doesn’t return key & value pairs but instead only returns indexes with values, it serves no purpose to use pairs over ipairs.

Each to their own. Thanks for the nice chat.

2 Likes


?? i dont understand, this is the code you wrote btw

edit: previous code did not have errors, its just that when i regened the thing never got deleted.

edit 2: i tried your ipairs loop too, did not work either.

ipair loop



image

Oh whoops, try the first method with the in pairs loop.

EDIT:

What was the error with that? Oh, and I believe you meant pairs.

EDIT 2: What is script.Parent in this case?

This will return what’s captured by (.*), not the name itself.
By instead matching for numbers at the start of the string, then capturing all characters after that, you can easily get the name and it doesn’t have to be hardcoded to work for “123redcamaro” only.

string.match(modelName, "%d+(.+)")
2 Likes

Oh I see the problem; you’ll want to switch it to this I believe:

local Vehiclesinspawn = script.Parent:GetChildren()
for i, v in pairs(Vehiclesinspawn) do
    if string.match(v.Name, "BlueFighter") then
        for x, vehicle in pairs(Vehicleinspawn) do
            if (vehicle.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude <= Range then
                vehicle:Destroy()
            end
        end
    end
end

EDIT: Though, what is it that you’re trying to do with the second for loop? In this case, it will destroy all vehicles within Range.

have no idea what this means but the problem was that i would have multiple “BlueFighters” and the script would try to delete one but they all had the same name, so to distinguish between them i would give them serials, but i want the code to ignore the serial and just find all the children with “bluefighter” in the name

image

I think you tried to get the children in pairs or something with that new code
edit: guys the first code worked except for the name issue
edit2: this code worked except nothing got deleted

local Vehiclesinspawn = script.Parent:GetChildren()
		if Vehiclesinspawn.Name == string.match("BlueFighter","(.*)BlueFighter") then 
			local x = script.Parent
			for x, Vehiclesinspawn in pairs(x) do
				if (Vehiclesinspawn.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude <= Range then
					Vehiclesinspawn:Destroy()
				end
			end
		end

Whoops, looks like there was a typo. Here:

local Vehiclesinspawn = script.Parent:GetChildren()
for i, v in pairs(Vehiclesinspawn) do
    if string.match(v.Name, "BlueFighter") then
        for x, vehicle in pairs(Vehiclesinspawn) do
            if (vehicle.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude <= Range then
                vehicle:Destroy()
            end
        end
    end
end

image
bruh moment

edit 69: this code works except models never get deleted even if it is in the spawn, the code that gets the name of the model is the issue and it is using the example @anon24371531 gave

local Vehiclesinspawn = script.Parent:GetChildren()
		if Vehiclesinspawn.Name == string.match("BlueFighter","(.*)BlueFighter") then 
			local x = script.Parent
			for x, Vehiclesinspawn in pairs(x) do
				if (Vehiclesinspawn.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude <= Range then
					Vehiclesinspawn:Destroy()
				end
			end
		end

edit 420: guys the idea is just to find all the models in the spawn system named “bluefighter” infront of the name of the model is a randomly generated number so that the script doesnt index like 3 identical models because they have the same name. so the script just ignores the serial and finds all the children of the model named “bluefighter” and removes them if it is in the spawn

so the issue is
if Vehiclesinspawn.Name == string.match("BlueFighter","(.*)BlueFighter") then

1 Like