I had this vehicle regen script that removes the vehicle if a player attempts to regenerate one and that vehicle is still in the spawn area.
Problem I had is that one would be outside the spawn, and one inside, and the script would break because the script didn’t know what to delete.
To circumvent this I attached a random number between 1 and 10000 and the model’s name when the vehicle is spawned, but now I’m wondering how to find the model name in the first place and not the number.
Example:
vehicle.name = “1337redcamaro” And to return the vehicles in the spawn:
name.find = ‘redcamaro’
I’m slightly confused about what this script is meant to do. Is there only ever meant to be one vehicle in the entire game per spawner or only one vehicle at a time in the spawn area?
If there’s only meant to be one vehicle in the game at all then you could keep a reference the active vehicle in the script rather than look for it again.
If there’s meant to be no more than one vehicle in the spawn area then you can just check the distance and you don’t need the name of the vehicle to do that. The distance checking part already seems to be there in your script.
There is an unlimited amount of vehicles allowed in the game, however, if a vehicle is left in the spawn area, it will be removed when the vehicle is next regenerated.
The problem is that the script breaks if a vehicle is outside the spawn and another vehicle is inside the spawn, and a player initiates the regeneration. The script is unable to distinguish between the two, so, to circumvent this i attached a random number between 1 and 10000 infront of the name. The problem i find now is just getting the name instead of the numbers and the name.
You can use string.match() with a bit of regex. (.*) will find anything before the string. If you place it on the other side of the string, you can find what comes after it.
local modelName = "123redcamaro"
local wordFound = string.match(modelName, "(.*)redcamaro")
print(wordFound)
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
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
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.
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:
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.
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
Thanks for taking the time to write this. These are all good and valid points and will take this into consideration
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.
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.