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 3 years ago

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

game:GetService("Teams")
local Players = game:GetService("Players")
function GetTycoonFromPlayer(Player)
    for i, v in pairs(script.Parent.Tycoons:GetChildren()) do
        if v:IsA("Model") then
            if v.Owner.Value == Player then
                return v
            end
        end
    end
    return nil
end

local tycoonBackups = {}
for i, v in pairs(script.Parent.Tycoons:GetChildren()) do
    tycoonBackups[v.Name] = v:Clone()
    if v:IsA("Model") then
        local Team = Instance.new("Team", game.Teams)
        Team.Name = v.Name
        Team.AutoAssignable = false
        Team.TeamColor = v.TeamColor.Value
    end
end
Players.PlayerAdded:Connect(function(Player)
    local PlayerCash = Instance.new("NumberValue", game.ServerStorage.PlayerMoney)
    PlayerCash.Name = Player.Name
    local PlayerOwnsTycoon = Instance.new("BoolValue", PlayerCash)
    PlayerOwnsTycoon.Name = "OwnsTycoon"
end)
Players.PlayerRemoving:Connect(function(Player)
    local PlayerCash = game.ServerStorage.PlayerMoney:FindFirstChild(Player.Name)
    if PlayerCash ~= nil then
        PlayerCash:Destroy()
    end
    local tycoon = GetTycoonFromPlayer(Player)
    local backup = tycoonBackups[tycoon.Name]:Clone()
    wait(1)
    backup.Parent = script.Parent.Tycoons
end)
0
Is there an error and what is not happening when the player leaves? AlmostADemon 50 — 3y
0
Error in question name. NillaTheCat12345 14 — 3y

2 answers

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

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

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

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:

function GetTycoonFromPlayer(Player)
    --Capture the immediate children of Tycoons
    for i, v in pairs(script.Parent.Tycoons:GetChildren()) do

        --Capture each of those children's children
        for ii, vv in pairs(script.Parent.Tycoons[v.Name]:GetChildren()) do
            if vv:IsA("Model") then
                if script.Parent.Tycoons[v.Name][vv.Name].Owner.Value == Player then
                    return v
                end
            end
        end
    end
    return -- don't need to specify nil here
end

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

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

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 — 3y
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 — 3y
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 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 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

game:GetService("Teams")
local Players = game:GetService("Players")
function GetTycoonFromPlayer(Player)
    for i, v in pairs(script.Parent.Tycoons:GetChildren()) do
        if v:IsA("Model") then
            if v.Owner.Value == Player.Name then
                return v
            end
        end
    end
    return nil
end

local tycoonBackups = {}
for i, v in pairs(script.Parent.Tycoons:GetChildren()) do
    tycoonBackups[v.Name] = v:Clone()
    if v:IsA("Model") then
        local Team = Instance.new("Team", game.Teams)
        Team.Name = v.Name
        Team.AutoAssignable = false
        Team.TeamColor = v.TeamColor.Value
    end
end
Players.PlayerAdded:Connect(function(Player)
    local PlayerCash = Instance.new("NumberValue", game.ServerStorage.PlayerMoney)
    PlayerCash.Name = Player.Name
    local PlayerOwnsTycoon = Instance.new("BoolValue", PlayerCash)
    PlayerOwnsTycoon.Name = "OwnsTycoon"
end)
Players.PlayerRemoving:Connect(function(Player)
    local PlayerCash = game.ServerStorage.PlayerMoney:FindFirstChild(Player.Name)
    if PlayerCash ~= nil then
        PlayerCash:Destroy()
    end
    local tycoon = GetTycoonFromPlayer(Player)
    local number = tonumber(tycoon.Name)
    local backup = table.find(tycoonBackups,tyccon.Name):Clone()
    wait(1)
    backup.Parent = script.Parent.Tycoons
end)
0
This did not work, trying to stop exception when player leaves without claiming tycoon NillaTheCat12345 14 — 3y
0
Is the Name of the tycoon a number? CrypxticDoge 135 — 3y
0
No.The name is Red Team NillaTheCat12345 14 — 3y
0
Exactly, so you can't index with the name CrypxticDoge 135 — 3y
View all comments (2 more)
0
Is the owner value the Name of the Player, or the UserID? CrypxticDoge 135 — 3y
0
It is the player object in game.Players NillaTheCat12345 14 — 3y

Answer this question