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
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
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
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
Your problem is that you are saying: if “BlueFighter” == “1253” or whatever is returned from the string.match which won’t work since you’re comparing the number with the name of the model.
Hmm. I should clarify a few things in my post too and possibly account for other situations providing a better and more relevant benchmark despite the facts being known enough already (later).
To keep this relevant I’ll address the OP now.
Relying on random serials isn’t good enough since they’re not guaranteed to be unique (if this is not relevant I might be misreading your OP) and it will certainly fail at one point where 2 or more objects might have the same name (although the odds are slim, they need to be accounted for).
it’s been a while since I’ve written lua in the browser, just providing this to let you know how to do just that (not in the context of the provided circumstances):
local toFind = "bluefighter";
local arr = {};
for _, obj in ipairs(container:GetChildren()) do
if (obj.Name:match(toFind)) then
table.insert(arr, obj);
end
end
print(arr);
local Vehiclesinspawn = script.Parent:GetChildren()
for i, v in ipairs(Vehiclesinspawn) do
if string.match(v.Name, "BlueFighter") then
if v.PlaneSeat.CFrame.p - Spawn.Frame.p).magnitude <= Range then
v:Destroy
end
end
end
EDIT: Where are the cars stored? As children to script.Parent?
local Vehiclesinspawn = script.Parent:GetChildren()
for i, v in ipairs(Vehiclesinspawn) do
if string.match(v.Name, "BlueFighter") then
if (v.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude <= Range then
v:Destroy()
end
end
end
pog with a few edits it worked
any ideas how to tackle this now bcuz i knew this would be an issue
is there a way to exclude randoms if that number is already generated
Ah I see what you mean now; I assumed you meant for loops overall haha. That makes sense, as you’d probably get a bit of optimization from not using a looping method that is made to include something else that doesn’t exist in the problem in the first place.
Yeah, absolutely. And yeah, thanks for the chat as well; let me know if you have something you’d want to ask me by just DM’ing me. Have a great week and weekend!
Oh, so these values aren’t permanently stored, but that they’re generated temporarily in the game server, just to be temporary unique numbers for each model? Are they unique to every single model in the game, or to every type of model?
not sure what this question is but ill go with it like jeopardy for $2000, every spawn names the plane the randomly generated number and the name of the plane. so if two different spawners generate the same serial it’s okay because the planes have different names