Answered by
4 years ago Edited 4 years ago
You have return nil
inside of your function GetTycoonFromPlayer
. So when this code is ran:
1 | local tycoon = GetTycoonFromPlayer(Player) |
tycoon will be nil IF any of the following are true:
script.Parent.Tycoons:GetChildren() returns a blank table (nothing is inside Tycoons)
The table returned from script.Parent.Tycoons:GetChildren() has no models
NOTE: GetChildren() only returns a table of the immediate children, not descendants
If none of the models inside Tycoons contains a model that has a value property set to the player
On the very next line, this code is trying to get the Name
property of nil:
1 | local backup = tycoonBackups [ tycoon.Name ] :Clone() |
My guess is that Tycoons has folders with the models you need in those folders. As I said, :GetChildren() only returns the immediate children, not the children of the children. There are a couple of ways to get around this such as using :GetDescendants() which will return the entire hierarchy inside Tycoons as a table, or you can just use another for loop like this:
01 | function GetTycoonFromPlayer(Player) |
03 | for i, v in pairs (script.Parent.Tycoons:GetChildren()) do |
06 | for ii, vv in pairs (script.Parent.Tycoons [ v.Name ] :GetChildren()) do |
07 | if vv:IsA( "Model" ) then |
08 | if script.Parent.Tycoons [ v.Name ] [ vv.Name ] .Owner.Value = = Player then |
Since you could potentially return nil, this will prevent any action taken if nil is found:
1 | local tycoon = GetTycoonFromPlayer(Player) |
3 | local backup = tycoonBackups [ tycoon.Name ] :Clone() |
5 | backup.Parent = script.Parent.Tycoons |
Hope this helps out.
[EDIT] I just re-read your post and noticed that it wasn't what I had guessed and that it was actually number 3 that was your problem. Thankfully I did cover that, you can ignore the for loop inside the for loop thing, but I'll keep it up as a reference in case you or anyone else runs into this problem.