Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Workspace.My Tycoon Kit.CoreScript:36: attempt to index nil with Name?

Asked by 4 years ago

I need help to fix in my tycoon once a player leaves without claiming a tycoon. My Code :

01game:GetService("Teams")
02local Players = game:GetService("Players")
03function GetTycoonFromPlayer(Player)
04    for i, v in pairs(script.Parent.Tycoons:GetChildren()) do
05        if v:IsA("Model") then
06            if v.Owner.Value == Player then
07                return v
08            end
09        end
10    end
11    return nil
12end
13 
14local tycoonBackups = {}
15for i, v in pairs(script.Parent.Tycoons:GetChildren()) do
View all 39 lines...
0
Is there an error and what is not happening when the player leaves? AlmostADemon 50 — 4y
0
Error in question name. NillaTheCat12345 14 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You have return nil inside of your function GetTycoonFromPlayer. So when this code is ran:

1local tycoon = GetTycoonFromPlayer(Player)

tycoon will be nil IF any of the following are true:

  1. script.Parent.Tycoons:GetChildren() returns a blank table (nothing is inside Tycoons)

  2. The table returned from script.Parent.Tycoons:GetChildren() has no models NOTE: GetChildren() only returns a table of the immediate children, not descendants

  3. 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:

1local 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:

01function GetTycoonFromPlayer(Player)
02    --Capture the immediate children of Tycoons
03    for i, v in pairs(script.Parent.Tycoons:GetChildren()) do
04 
05        --Capture each of those children's children
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
09                    return v
10                end
11            end
12        end
13    end
14    return -- don't need to specify nil here
15end

Since you could potentially return nil, this will prevent any action taken if nil is found:

1local tycoon = GetTycoonFromPlayer(Player)
2if tycoon then --Checks to make sure tycoon isn't nil
3    local backup = tycoonBackups[tycoon.Name]:Clone()
4    wait(1)
5    backup.Parent = script.Parent.Tycoons
6end

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.

0
You may want to place script.Parent.Tycoons inside a variable so it can easily be referenced SteelMettle1 394 — 4y
0
I've copied code but did some edits. Line 8 : if script.Parent.Tycoons[v.Name].Owner.Value == Player then. Line 9 : return v (the tycoon) NillaTheCat12345 14 — 4y
0
Yeah, from the layout of your code you'd want to send back the tycoon, not the model. I overlooked it on my part, but I'll edit the answer anyway. SteelMettle1 394 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

When you index a value from a table, it has to be a number. Use the tonumber function and convert the string into a number. Remember, if tycoon.Name is not a number, it will throw an exception.

Here is what your code should look like

01game:GetService("Teams")
02local Players = game:GetService("Players")
03function GetTycoonFromPlayer(Player)
04    for i, v in pairs(script.Parent.Tycoons:GetChildren()) do
05        if v:IsA("Model") then
06            if v.Owner.Value == Player.Name then
07                return v
08            end
09        end
10    end
11    return nil
12end
13 
14local tycoonBackups = {}
15for i, v in pairs(script.Parent.Tycoons:GetChildren()) do
View all 40 lines...
0
This did not work, trying to stop exception when player leaves without claiming tycoon NillaTheCat12345 14 — 4y
0
Is the Name of the tycoon a number? CrypxticDoge 135 — 4y
0
No.The name is Red Team NillaTheCat12345 14 — 4y
0
Exactly, so you can't index with the name CrypxticDoge 135 — 4y
View all comments (2 more)
0
Is the owner value the Name of the Player, or the UserID? CrypxticDoge 135 — 4y
0
It is the player object in game.Players NillaTheCat12345 14 — 4y

Answer this question