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

My Script For Loading A Saved Tycoon Isn't Working?

Asked by 7 years ago
Edited by M39a9am3R 7 years ago

This script is having a glitch? I designed it to load a tycoons data when is has data, It loads until it get's to a certain item? then it says: 08:16:19.311 - Workspace.Zednov's Tycoon Kit.Tycoons.Blue Food Tycoon.PurchaseHandler:273: attempt to index field '?' (a nil value)

Can you help me?

    --[[
        All configurations are located in the "Settings" Module script.
        Please don't edit this script unless you know what you're doing.
    --]]
    local gp = game:GetService("GamePassService")
    local id = 780368541
    local Objects = {}
    local TeamColor = script.Parent.TeamColor.Value
    local Settings = require(script.Parent.Parent.Parent.Settings)
    local Money = script.Parent.CurrencyToCollect
    local Debris = game:GetService('Debris')
    local Stealing = Settings.StealSettings
    local CanSteal = true -- don't change or else you won't be able to steal currency

    script.Parent.Essentials.Spawn.TeamColor = TeamColor
    script.Parent.Essentials.Spawn.BrickColor = TeamColor

    function Sound(part,id)
        if part:FindFirstChild('Sound') then
            return
        else
            local Sound = Instance.new('Sound',part)
            Sound.SoundId = "rbxassetid://"..tostring(id)
            Sound.Volume = 0.25
            Sound:Play()
            delay(Sound.TimeLength, function()
                Sound:Destroy()
            end)
        end
    end

    --Parts that fall into the collector(s) get processed
    for i,v in pairs(script.Parent.Essentials:GetChildren()) do
        if v.Name == "PartCollector" then
            v.Touched:connect(function(Part)
                if Part:FindFirstChild('Cash') then
                    if script.Parent.QuickCash.Value == true then
                        local stats = game.ServerStorage.PlayerMoney:FindFirstChild(script.Parent.OwnerName.Value)
                        stats.Value = stats.Value + Part.Cash.Value
                    else
                    Money.Value = Money.Value + Part.Cash.Value

                    end 
                Debris:AddItem(Part,0.1)
                end
            end)
        end
    end

    --Player Touched Collector processor
    deb = false
    script.Parent.Essentials.Giver.Touched:connect(function(hit)
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player ~= nil then
            if script.Parent.Owner.Value == player then
                if hit.Parent:FindFirstChild("Humanoid") then
                    if hit.Parent.Humanoid.Health > 0 then
                        if deb == false then
                            deb = true
                            script.Parent.Essentials.Giver.BrickColor = BrickColor.new("Bright red")
                            local Stats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
                            if Stats ~= nil then 
                            Sound(script.Parent.Essentials, Settings.Sounds.Collect)
                            Stats.Value = Stats.Value + Money.Value
                            Money.Value = 0
                            wait(1)
                            script.Parent.Essentials.Giver.BrickColor = BrickColor.new("Sea green")
                            deb = false
                            end
                        end
                    end
                end
            elseif Stealing.Stealing then -- if player isn't owner and stealing is on
                if CanSteal == true then
                    CanSteal = false
                    delay(Stealing.PlayerProtection, function()
                        CanSteal = true
                    end)
                    if hit.Parent:FindFirstChild("Humanoid") then
                        if hit.Parent.Humanoid.Health > 0 then
                            local Stats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
                            if Stats ~= nil then
                                local Difference = math.floor(Money.Value * Stealing.StealPrecent)
                                Sound(script.Parent.Essentials, Settings.Sounds.Collect)
                                Stats.Value = Stats.Value + Difference
                                Money.Value = Money.Value - Difference
                            end
                        end
                    end
                else
                    Sound(script.Parent.Essentials, Settings.Sounds.Error)
                end
            end
        end
    end)

    script.Parent:WaitForChild("Buttons")
    for i,v in pairs(script.Parent.Buttons:GetChildren()) do
        spawn(function()
        if v:FindFirstChild("Head") then

            local ThingMade = script.Parent.Purchases:WaitForChild(v.Object.Value)
            if ThingMade ~= nil then
                Objects[ThingMade.Name] = ThingMade:Clone()
                ThingMade:Destroy()
            else
                --//Button doesn't have object, remove it
                error('Object missing for button: '..v.Name..', button has been removed')
                v.Head.CanCollide = false
                v.Head.Transparency = 1
            end

            if v:FindFirstChild("Dependency") then --// if button needs something unlocked before it pops up
                for i,c in pairs(v:GetChildren()) do
                            if c:FindFirstChild("NamingGui") then
                            c.NamingGui.Frame.Visible = false
                            end
                    if c:IsA("Part") then
                c.CanCollide = false
                c.Transparency = 1
                    end
                end
                coroutine.resume(coroutine.create(function()
                    if script.Parent.PurchasedObjects:WaitForChild(v.Dependency.Value) then
                        if Settings['ButtonsFadeIn'] then
                            for i=1,20 do
                                wait(Settings['FadeInTime']/20)
                                for i,c in pairs(v:GetChildren()) do
                                    if c:IsA("Part") then
                                        if c.Name ~= "Head" then
                                       c.Transparency = c.Transparency - 0.05
                                        end 
                                    end
                                end
                            end
                        end
                        for i,c in pairs(v:GetChildren()) do
                            if c:FindFirstChild("NamingGui") then
                                c.NamingGui.Frame.Visible = true
                            end
                            if c:IsA("Part") then
                        c.CanCollide = true
                        if c.Name == "Head" then
                        c.Transparency = 1
                        else
                            c.Transparency = 0
                        end
                            end
                        end
                    end
                end))
            end

            v.Head.Touched:connect(function(hit)
                local player = game.Players:GetPlayerFromCharacter(hit.Parent)
                if v.Head.CanCollide == true then
                    if player ~= nil then
                        if script.Parent.Owner.Value == player then
                            if hit.Parent:FindFirstChild("Humanoid") then
                                if hit.Parent.Humanoid.Health > 0 then
                                    local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
                                    if PlayerStats ~= nil then
                                        if (v:FindFirstChild('Gamepass')) and (v.Gamepass.Value >= 1) then
                                            if game:GetService("MarketplaceService"):PlayerOwnsAsset(player,v.Gamepass.Value) then
                                                Purchase({[1] = v.Price.Value,[2] = v,[3] = PlayerStats})
                                            else
                                                game:GetService('MarketplaceService'):PromptPurchase(player,v.Gamepass.Value)
                                            end
                                        elseif (v:FindFirstChild('DevProduct')) and (v.DevProduct.Value >= 1) then
                                            game:GetService('MarketplaceService'):PromptProductPurchase(player,v.DevProduct.Value)
                                        elseif PlayerStats.Value >= v.Price.Value then
                                            Purchase({[1] = v.Price.Value,[2] = v,[3] = PlayerStats})
                                            Sound(v, Settings.Sounds.Purchase)
                                        else
                                            Sound(v, Settings.Sounds.ErrorBuy)
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end)
            end
        end)
    end


    --// Loads Players Tycoon Data
    script.Parent.Entrance["Touch to claim!"].Head.Touched:connect(function(hit)
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player ~= nil then
        local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
        if PlayerStats ~= nil then
            local ownstycoon = PlayerStats:FindFirstChild("OwnsTycoon")
            if ownstycoon ~= nil then
                if ownstycoon.Value == nil then
                    if script.Parent.Owner.Value == nil then
                        if hit.Parent:FindFirstChild("Humanoid") then
                            if hit.Parent.Humanoid.Health > 0 then
                                if not player.PlayerGui:FindFirstChild("Gui") then
                                --Gui Script
                                local gui = script.Gui:Clone()
                                gui.Parent = player.PlayerGui
                                gui.Frame.Load.MouseButton1Down:connect(function()
                                    for i,v in pairs(player.TycoonData:GetChildren()) do
                                        if player.TycoonData.Mine.Value == false then
                                            gui.Frame.Load.Text = "No Data Found"
                                            wait(1)
                                            gui.Frame.Load.Text = "Load Tycoon"
                                        else
                                            local chills = player.TycoonData:GetChildren()
                                            for i = 1,#chills do
                                                wait(0.1)
                                                if chills[i].Value == true then
                                                    local item = Objects[chills[i].Name]:Clone()
                                                    if item ~= nil then
                                                    item.Parent = script.Parent.PurchasedObjects
                                                    else
                                                        print(item.Name.." is Nil")
                                                        item:remove()
                                                    end
                                                   end
                                                local buttons = script.Parent.Buttons:GetChildren()
                                                for i = 1,#buttons do
                                                    wait()
                                                    if player.TycoonData:FindFirstChild(buttons[i].Object.Value) then
                                                        for i,v in pairs(buttons[i]:GetChildren()) do
                                                            if v:IsA("Part") then
                                                            v.Transparency = 1
                                                            v.CanCollide = false
                                                            end
                                                            if v:FindFirstChild("NamingGui") then
                                                                v.NamingGui.Frame.Visible = false
                                                            end
                                                        end
                                                    end
                                                end
                                                end
                                            end
                                        end
                                    --Gui Script
                                    end)
                                end
                            end
                        end
                    end
                end
            end
        end
    end

0
Why did it mix my code up :O starblasto 43 — 7y
0
What tycoon kit is this from DennisSavoie1234 0 — 5y

1 answer

Log in to vote
0
Answered by 7 years ago

There's way to much code to look at here, so I'll just tell you what your error message means. Your error means that either you haven't defined PurchaseHandler (as in it doesn't exist), or it could also mean that PurchaseHandler hasn't loaded into the game yet. Assuming it's the second one, you can utilize WaitForChild().

Ad

Answer this question