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

[Solved]How many coroutines should I be limiting myself to when scripting this tycoon?

Asked by 5 years ago
Edited 5 years ago

So I recently had a problem involving a loop and a touched event. When I looped through the buttons and said when this button is touched do this, it did not work. So now I am cloning a module script into each button with an event in it:

local function findBaseplate(buildLevel, serverTycoon)

    for i,v in pairs(serverTycoon:GetChildren()) do

        if v.IsABaseplate.Value then
            if v.RequiredLevel.Value == buildLevel then

                return v

            end
        end
    end
end

local function createDependentButtons(itemName, tycoon)

    if #tycoon.Buttons:GetChildren() >= 1 then

        for i,v in pairs(tycoon.Buttons:GetChildren()) do

            wait()

            if itemName == v.Dependency.Value then

                v.Head.Transparency = 0
                v.Head.CanCollide = true

            end
        end
    end
end

local debounce = true

script.Parent.Touched:Connect(function(hit)

    if script.Parent.CanCollide then
        if hit.Parent:FindFirstChild("Humanoid") then
            if hit.Parent.Humanoid.Health > 0 then

                local tycoon = script.Parent.Parent.Parent.Parent   
                local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
                local plrName = plr.Name

                if tycoon.IsOwned.Value then
                    if plrName == tycoon.Owner.Value then   

                        local plrInfo = game.ServerStorage:FindFirstChild(plrName):WaitForChild("PlayerInfo")
                        local plrMoney = plrInfo.Money
                        local buildLevel = plrInfo.BuildLevel.Value 
                        local serverTycoon = game.ServerStorage:FindFirstChild("Model"..tycoon.Color.Value.."Tycoon")
                        local baseplate = findBaseplate(buildLevel, serverTycoon)
                        local baseplateName = baseplate.Name
                        local tycoonBaseplate = tycoon:FindFirstChild("Baseplates"):WaitForChild(baseplateName)
                        local serverBaseplate = serverTycoon:WaitForChild(baseplateName)
                        local price = script.Parent.Parent.Price

                        if plr then
                            if debounce then

                                debounce = false    

                                if plrMoney.Value >= price.Value then

                                    local purchasableItemName = script.Parent.Parent.Item.Value
                                    local itemType = script.Parent.Parent.ItemType

                                    plrMoney.Value = plrMoney.Value - price.Value 

                                    if itemType.Value == "Dropper" then

                                        local copy = serverBaseplate.PurchasableItems:FindFirstChild(purchasableItemName)

                                        copy.Parent = tycoonBaseplate.PurchasedItems

                                        local runDropperFunction = require(game.ServerStorage.ModuleScripts.RunDropper)

                                        wait()

                                        runDropperFunction(copy)                            

                                        script.Parent.CanCollide = false
                                        script.Parent.Transparency = 1                  

                                    elseif itemType.Value == "Upgrader" then

                                        local copy = serverBaseplate.PurchasableItems:FindFirstChild(purchasableItemName)                   

                                        copy.Parent = tycoonBaseplate.PurchasedItems

                                        local runUpgrader = require(game.ServerStorage.ModuleScripts.UpgraderScript)

                                        wait()

                                        runUpgrader(copy)                   

                                        script.Parent.CanCollide = false
                                        script.Parent.Transparency = 1  

                                    else

                                        print("Error detecting the type of object you are trying to purchase.")
                                        plr:Kick("Something went wrong please rejoin")                  

                                    end

                                    createDependentButtons(purchasableItemName, tycoon)

                                else

                                    print(plrName.." does not have enough cash for the purchase")

                                end

                                wait(2)

                                debounce = true

                            end
                        end
                    end 
                end 
            end
        end
    end
end)

return true 

First I would like to know if this is the best way or if there is a better way. Second I have a module script which is required by the one above. Here it is:

local function runUpgrader(upgrader)

    spawn(function()

        local upgradeAmount = upgrader:FindFirstChild("UpgradeAmount")
        local upgradeType = upgrader:FindFirstChild("UpgradeType")

        if upgradeAmount and upgradeType then

            upgrader.UpgraderPart.Touched:Connect(function(hit)

                if hit.Name == "Ore" then
                    if hit:FindFirstChild("Cash") then

                        local cash = hit.Cash

                        if upgradeType.Value == "Addition" then

                            cash.Value = hit.Cash.Value + upgradeAmount

                        elseif upgradeType.Value == "Multiplication" then

                            cash.Value = cash.Value * upgradeAmount

                        elseif upgradeType.Value == "ModulusAddition" then

                            print("Not currently a valid upgrade method")

                        elseif upgradeType.Value == "ModulusMultiplication" then

                            print("Not currently a valid upgrade method")

                        end
                    end
                end
            end)
        end
    end)
end

return runUpgrader

I am wondering if it is a good idea to have that many coroutines going on in my game and if it is not encouraged. Because before this I was doing the same thing as the buttons. I was cloning the module script into the dropper and having the event in the module script. Is there a better way? If the better way is to use coroutines then I would definitely use it for the button module script as well. Please tell me if the methods I am using are good or bad and what is the best way to go about this kind of thing. Thanks!

0
The coroutine is the spawn near the beginning of the second script in the question. User#21908 42 — 5y

Answer this question