Let's take a step back and think here.
What could we do differently besides a loop, to make it more effective?
First
We need to create a new script in Workspace that will set the owner's name to nothing in the event he leaves. This is simple, so I won't go into to much detail other than the comments I leave.
01 | function FactoryFind(place, name) |
02 | for _,v in pairs (place:GetChildren()) do |
03 | if v.Name = = "Factory" then |
04 | if v.Factory.OwnerName.Value = = name then |
05 | v.Factory.OwnerName.Value = "" |
13 | game.Players.PlayerLeaving:connect( function (oldPlayer) |
14 | FactoryFind(Workspace, oldPlayer.Name) |
Now,
All we need to do is edit your current script to used the changed event. Let's first create the event:
Note: We are going to keep your regen and variables for consistency.
01 | model = script.Parent.Factory |
03 | ownerName = script.Parent.Factory.OwnerName |
08 | model = backup:clone() |
09 | model.Parent = script.Parent |
13 | ownerName.Changed:connect( function (onChange) |
14 | if ownerName.Value = = "" then |
Finally..
In theory, the code I have supplied you, and with the ways mentioned, you can figure out how to consider other possible means yourself in the future. Just need to always keep thinking, and remember, try to solve the problem, not just cover it.
Note: I have done absolutely no testing with this. I am simply assuming the code given will work, as I do not have studio right now to run any tests on.