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

How to make resources regenerate overtime?

Asked by 4 years ago

I am busy with an open source survival game and would love to have my resources regenerate overtime.

I myself have no clue how to do such a thing and thought the best place to get help is here.

I have found 2 scripts that might could help me.

Script 1 found at: https://developer.roblox.com/en-us/recipes/Regenerate-Models-

    local model, button, enabled = Workspace.Model, Workspace.Button, true
    local clone = model:Clone()

    button.Touched:connect(function(hit)
        if game.Players:FindFirstChild(hit.Parent.Name) and enabled then
            enabled = false
            model:Destroy()
            wait(5)
            local lclone = clone:Clone()
            lclone.Parent = Workspace
            model = lclone
            enabled = true
        end
    end)

Script 2 found at: https://www.roblox.com/games/429735644/DUSK-UNCOPYLOCKED

objects_to_regen = {
    --Name, regen time, model(false) Part(true)
    {'Rock', {120,240}, true},
    {'Large Rock', {360,600}, true},
    {'Stone', {180,300}, true},
    {'Wheat', {120,240}, true},
    {'Kelp', {120,240}, true},
    {'Pond Water', {240,320}, true},
    {'Corn', {240,360}, true},
    {'Mushroom', {80,160}, true},
    {'Onion', {240,360}, true},
    {'Pumpkin', {300,420}, true},
    {'Rice', {80,140}, true},
    -- models
    {'Berry Bush', {180,240}, false},
    {'Tree', {180, 300}, false},
    {'Large Tree', {360,600}, false},
    {'Fancy Tree', {240,360}, false},
    {'Bush', {120,240}, false},
    {'Coal Rock', {180, 300}, false},
    {'Copper Rock', {240,360}, false},
    {'Iron Rock', {440,600}, false},
    {'Adurite Rock', {900,1300}, false},
    {'Magnatite Rock', {3600,6000}, false},
}

function check(input)
    for i,v in pairs(objects_to_regen) do
        if v[1] == input then
            return {true, v[2], v[3]}
        end
    end
    return {false, nil, nil}
end

function part(object, wt)
    local new = object:Clone()
    object.AncestryChanged:connect(function()
        --print(object.Name, ' was removed, regen.')
        wait(math.random(wt[1], wt[2]))
        new.Parent = workspace
        if new.Name == "Water" then
            local val = Instance.new("IntValue",new)
            val.Value = 400
            val.Name = "Amount"
        end
        part(new, wt)
    end)
end

function model(object, wt)
    local new = object:Clone()
    object.ChildRemoved:connect(function()
        if object:FindFirstChild'DB' then return end
        Instance.new('ObjectValue', object).Name = 'DB'
        --print('An object from ',object.Name, ' was removed, regen.')
        wait(math.random(wt[1], wt[2]))
        object:Destroy()
        new.Parent = workspace
        new:MakeJoints()
        if new.Name == "Bush" then
            local weld = Instance.new("ManualWeld")
            weld.Part0 = new:WaitForChild("Leaves")
            weld.Part1 = new:WaitForChild("Wood")
            weld.C0 = new:WaitForChild("Leaves").CFrame:inverse() * new:WaitForChild("Wood").CFrame 
            weld.Parent = new:WaitForChild("Leaves")
        end
        if new.Name == "Tree" then
            for i,v in pairs(new:GetChildren()) do
                if v.Name == "Orangeberry" then
            local weld = Instance.new("ManualWeld")
            weld.Part0 = v
            weld.Part1 = new:WaitForChild("Leaves")
            weld.C0 = v.CFrame:inverse() * new:WaitForChild("Leaves").CFrame    
            weld.Parent = v
                end
            end
        end
        model(new, wt)
    end)
end

for i,v in pairs(workspace:GetChildren()) do
    local test = check(v.Name) 
    if test[1] then
        if test[3] then
            part(v, test[2])
        else
            model(v, test[2])
        end
    end
end

The most important thing here is that resources such as berries, iron ores, trees, sand, rocks and other resource types in the future can regenerated and gathered by new or existing players.

Already thanks for the help.

Link to the open source game: https://devforum.roblox.com/t/os-game-survival-game-prototype-0-0-2/347122

Answer this question