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

Why is my loop randomly stopping and stopping the rest of the script?

Asked by 7 years ago
Edited 7 years ago

I have a chest unlocking system. Here is the code. unlock is the BillboardGui shown while unlocking. items is a table of items the chest can pick from.

local plr = script.Parent.Parent.Parent.Parent.Parent
local data = plr:WaitForChild("PlayerData")
local DS = game:GetService("DataStoreService"):GetDataStore("GameData")
local purchases = DS:GetAsync("ShovelsBought")
local itemdata = script.Parent.Parent.Parent.Items:FindFirstChild(script.Parent.Parent.itemName.Value)

script.Parent.MouseButton1Click:connect(function()
    if data.Leaderstats.Currency.Value >= itemdata.Price.Value then
        purchases = purchases + 1
        DS:SetAsync("ShovelsBought",purchases)
        purchases = DS:GetAsync("ShovelsBought")
        data.Leaderstats.Currency.Value = data.Leaderstats.Currency.Value - itemdata.Price.Value
        -- For use with shovels.  This part works fine.  Scroll to the line where it says itemdata.Type.Value == "Crate".
        if itemdata.Type.Value == "Shovel" or itemdata.Type.Value == "Trail" then
        local newitem = data.Inventory.Basic:Clone()
        newitem.Name = script.Parent.Parent.itemName.Value
        newitem.Value = true
        newitem.Parent = data.Inventory
        local scr = script.Parent.Parent.Parent.Frame.Scripts.Equip:Clone()
        scr.Parent = script.Parent
        scr.Disabled = false
        script.Parent.Text = "Equip"
        script:Destroy()
        elseif itemdata.Type.Value == "Crate" then --Starts here.
            local items = {} --Table for containing possible chest items

            for _,v in pairs(itemdata:GetChildren()) do
                if v.Name == "ChestItem" then
                    table.insert(items,v) --Inserts all chest items into the table.
                end
            end
            local unlock = game.ServerStorage.UnlockingIcon:Clone() --Makes the Billboardgui
            unlock.Parent = plr.Character.Head
            --[[for i = 1,12 do --Temporarily noted.  This works fine.
                print("0a"..i)
                unlock.ExtentsOffset = unlock.ExtentsOffset + Vector3.new(0,0.5,0)
                wait(0.2)
            end]]
            local runs = 0
            for i = 1,30 do --This is where problem starts.
                local tempitem = items[math.random(1,#items)].Value
                runs = runs +1
                unlock.Frame.ItemImg.Image = "rbxassetid://"..tempitem.ImageId.Value
                print(runs)
                wait(0.15)
            end
            print(2)
        end
    end
end)

The problem is, the loop is randomly stopping, and stopping the rest of the script. I added runs for debugging purposes, and each time I run it, I see it stop at a random number, then the rest of the script doesn't run.

For instance: Run 1 output:

1
2
3
4
5

Run 2 output:

1
2
3

Etc. Any help?

0
It sounds like a nil-value-access, but I would expect it to give an error in that case (which you don't mention). Right after "runs = runs + 1", put in "print(tmpItem, tmpItem and tmpItem.ImageId, tmpItem and tmpItem.ImageId and tmpItem.ImageId.Value)". If it prints out 'nil' anywhere, that's the problem. chess123mate 5873 — 7y

Answer this question