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

why is there and error on line 33 something to do with "FindFirstChild"?

Asked by 3 years ago
local player = script.Parent.Parent.Parent.Parent.Parent.Parent
Cost = 5

function checkForChocolateMilk()
    local found = false
    for i, v in pairs(player.Backpack:GetChildren()) do
        if v.Name == "ChocolateMilk"
        then
            found = true
        end
    end
    for i, v in pairs(player.Character:GetChildren()) do
        if v.Name == "ChocolateMilk"
        then
            found = true
        end
    end

    if found == true
    then
        return true
    else
        return false
    end
end

script.Parent.MouseButton1Click:Connect(function()
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats
    then
        local Points = leaderstats:FindFirstChild("Points")
    end
    local Points = leaderstats:FindFirstChild("Points")
        if Points then
            local hasChocolateMilk = checkForChocolateMilk()()
        if Points.Value >= Cost and hasChocolateMilk == false then
            Points.Value = Points.Value - Cost
            local ChocolateMilk = game.ServerStorage.ChocolateMilk:Clone()
            ChocolateMilk.Parent = player.Backpack
        end
        local found = false
        for i, v in pairs(player.Backpack:GetChildren()) do
        if v.Name == "ChocolateMilk"
                then
                found = true
            end
            if found == true
            then
                script.Parent.Parent.Parent.ChocolateMilkOwned.Visible = true
        script.Parent.Roundify.ImageColor3 = Color3.fromRGB(255, 44, 47)

            end
        end
    end
end)
0
whats the error? Dan_PanMan 227 — 3y
0
Players.diggerbear1.PlayerGui.Store.MainShop.Frame.Frame.BuyChocolateMilk.Script:33: attempt to index nil with 'FindFirstChild' diggerbear1 32 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

Seems like you're trying to only set Points if leaderstats exists, which is not what your code is currently doing. Looks like you have some confusion in general about if blocks. Change the MouseButton function to something like this:

script.Parent.MouseButton1Click:Connect(function()
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
        local Points = leaderstats:FindFirstChild("Points")
        if Points then
            local hasChocolateMilk = checkForChocolateMilk()
            if Points.Value >= Cost and hasChocolateMilk == false then
                Points.Value = Points.Value - Cost
                local ChocolateMilk = game.ServerStorage.ChocolateMilk:Clone()
                ChocolateMilk.Parent = player.Backpack
            end
            local found = false
            for i, v in pairs(player.Backpack:GetChildren()) do
                if v.Name == "ChocolateMilk" then
                    found = true
                    break
                end
            end
            if found == true then
                script.Parent.Parent.Parent.ChocolateMilkOwned.Visible = true
                script.Parent.Roundify.ImageColor3 = Color3.fromRGB(255, 44, 47)
            end
        end
    end
end)

You also should break out of the for look when what you want is found, as seen above

0
i know it says its the answer but it isnt it doesnt work for some reason diggerbear1 32 — 3y
Ad
Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
3 years ago

In your code, you check if leaderstats has been created and then create the Points IntValue, however directly after it you check for points outside of the statement, therefore nullifying the purpose of the if statement to catch the potential error.

tl;dr: remove line 33

Answer this question