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

[SOLVED] What's wrong here? Index nil Value.

Asked by 5 years ago
Edited 5 years ago

I get this error:

Workspace.SellPlatform.SellScript:29: attempt to index a nil value

For this script:

-- When a player touches the parent object, their items will be sold for gold
local sellPart = script.Parent

-- Gives the player gold for each item they have
local function sellItems(playerItems, playerGold)
    -- Gives players 100 pieces of gold for each item
    local totalSell = (playerItems.Value * 100)
    playerGold.Value = playerGold.Value + totalSell
    playerItems.Value = 0
end

local function onTouch(partTouched)
    -- Looks for a Humanoid
    local character = partTouched.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")

    -- If a humanoid is found, gets leaderstats and calls sellItems function
    if humanoid then
        -- Get the player, so changes can be made to  the player's leaderstats      
        local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
        local playerStats = player:FindFirstChild("leaderstats")
        local playerSpaces = playerStats:FindFirstChild("Spaces")
        local playerItems = playerStats:FindFirstChild("Items")
        local playerGold = playerStats:FindFirstChild("Gold")
        local playerStats2 = player:FindFirstChild("Stats")
        local playerSold = playerStats2:FindFirstChild("Sold")
        local playerSoldRequired = playerStats2:FindFirstChild("SoldRequired")  
        local playerLevel = playerStats2:FindFirstChild("Level")
        local playerLevelGui = player.PlayerGui:FindFirstChild("Stats").Level           

        -- Sells Items and then changes the player's spaces and money
        playerSold.Value = playerSold.Value + playerItems.Value
        sellItems(playerItems, playerGold)
            if playerSold.Value >= playerSoldRequired.Value and playerSpaces.Value >= playerItems.Value then
                playerLevel.Value = playerLevel.Value + 1
                playerLevelGui.Visible = true
                playerSoldRequired.Value = playerLevel.Value * 100 + playerSoldRequired.Value
                playerSold.Value = 0
            end


    end
end

sellPart.Touched:Connect(onTouch)
0
Yeah, It appears in PlayerGui, but idk...how can i move them there using a script? ady1111 8 — 5y
0
Yes..But this "index nil value" kills me.. ady1111 8 — 5y
0
Stats is not a valid member of PlayerGui ady1111 8 — 5y
0
Stats is not a valid member of PlayerGui. I think using StarterGui instead makes it visible to everyone, and i only want the localplayer to see the Gui when he meets the requirement. ady1111 8 — 5y
View all comments (24 more)
0
Ignore the "Stats is not a valid member of PlayerGui." from above. ady1111 8 — 5y
0
AradIsHere, please stop spreading misinformation. User#25115 0 — 5y
0
This is the starterGui : http://prntscr.com/ml3nke ady1111 8 — 5y
0
The frame doesn't turn visible... I tried with "local playerLevelGui = game.StarterGui.Stats.Level" and then "playerLevelGui.Visible = true" ady1111 8 — 5y
0
It should not have worked for you. That code is just modifying what replicates to clients when they die or join. Now stop spreading misinformation like I asked, AradIsHere. User#25115 0 — 5y
0
thats why i dont post answers... AradIsHere 2 — 5y
0
Why'd you remove your comments? Even if they were wrong, they were important for context. User#25115 0 — 5y
0
ady1111, I'll help you out. Is the line the error occurs on the same line as line twenty-nine in the script you posted? If not, please tell me the line that is erroring. User#25115 0 — 5y
0
Yes, line 29 is the problem ady1111 8 — 5y
0
Ok, for debugging purposes, I'd like you to, before the line that's erroring, write: print(player); print(player.PlayerGui); print(player.PlayerGui:FindFirstChild("Stats")) User#25115 0 — 5y
0
Tell me if one of the above prints nil. User#25115 0 — 5y
0
1.ady1111 2.PlayerGui 3.nil ady1111 8 — 5y
0
Ok, FindFirstChild returns nil when an item with the name matching the string passed as the argument is not found. User#25115 0 — 5y
0
This is the PlayerGui: http://prntscr.com/ml41dx ady1111 8 — 5y
0
Right, and that confuses me because this should only be erroring if FindFirstChild returns nil, but FindFirstChild shouldn't be returning nil. Do you mind commenting out that line and telling me what happens? User#25115 0 — 5y
0
What do you mean? ady1111 8 — 5y
0
Basically, what happens when you remove that line of code? User#25115 0 — 5y
0
Workspace.SellPlatform.SellScript:36: attempt to index global 'playerLevelGui' (a nil value) ady1111 8 — 5y
0
Ok, now we know for sure that the FindFirstChild returning nil is causing the issue. User#25115 0 — 5y
0
I did some research, and, apparently, the server can't access things in the PlayerGui. You're going to have to modify that gui locally. User#25115 0 — 5y
0
Remote events are your only option in this scenario. User#25115 0 — 5y
0
So how should i do it? If the line 34 condition is met, the gui becomes visible...don't know how...I always have trouble with this error. ady1111 8 — 5y
0
Managed to do it with Remote Events, thank you! ady1111 8 — 5y
0
Items in `StarterGui` are cloned locally, so you'll have to use remotes to be able to accomplish what you're trying to do. TheeDeathCaster 2368 — 5y

Answer this question