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

Add increment till capped?

Asked by 5 years ago
Edited 5 years ago

Lets say I have a Value named 'Level', a Value named 'Profit', and a Value named 'MaxProfit' How would I make it so every time Level goes up by 1, Profit is increased by a number, which slowly gets smaller as Profit gets closer to Max Profit?

For example Max Profit is 100 I start out at Level 1, and when I reach level 2, Profit goes up by 10, then 9, 8, etc.. till it finally caps out

2 answers

Log in to vote
0
Answered by 5 years ago

You can do this in a variety of different mathematical equations, but generally, its probably best to check if the profit is less than maxprofit before using any math. After this, the level of the player will determine the enacted the function both of which use if-then clauses.

The variables in this Local Script are defined in the other Script below (placed in StarterGui)

local player = game.Players.LocalPlayer
local Data = player:WaitForChild("Data")

local level = Data:WaitForChild("Level")
local profit = Data:WaitForChild("Profit")

local MaxProfit = 100
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")

level.Changed:Connect(function()
local Level = level.Value
local Profit = profit.Value
    if Profit < MaxProfit then
        if Level <= 10 then
            local create = math.abs(11 - Level)
            remote:FireServer(create, Level)
        elseif Level > 10 then
            remote:FireServer(1, Level)
        end
    end
end)

The reason I chose 10 as the final number for levels to decrease is that after this, your value will reach 0 (and no profit is added) but the function would not have reached the maxprofit value of 100 (it would be 55).

I also combined this with a DataStore, though I'm not sure if you already have one, as well as a Remote Event (named "RemoteEvent" within Replicated Storage) so that the function fires when the values change on the Client.

Server Script (placed in ServerScriptService)

local DSS = game:GetService("DataStoreService")
local Storage = DSS:GetDataStore("Data")
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")

game.Players.PlayerAdded:Connect(function(player)
    local LoadArray = Storage:GetAsync(player.UserId)

    if LoadArray ~= nil then
        local datafolder = Instance.new("Folder", player)
        datafolder.Name = "Data"

        local level = Instance.new("IntValue", datafolder)
        level.Name = "Level"
        level.Value = LoadArray[1]

        local profit = Instance.new("IntValue", datafolder)
        profit.Name = "Profit"
        profit.Value = LoadArray[2]
    else
        local datafolder = Instance.new("Folder", player)
        datafolder.Name = "Data"

        local level = Instance.new("IntValue", datafolder)
        level.Name = "Level"
        level.Value = 1

        local profit = Instance.new("IntValue", datafolder)
        profit.Name = "Profit"
        profit.Value = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local SaveArray = {player.Data.Level.Value, player.Data.Profit.Value}
    Storage:SetAsync(player.UserId, SaveArray)
end)

remote.OnServerEvent:Connect(function(player, add, level)
    local original = player.Data.Profit.Value
    player.Data.Profit.Value = original + add
    player.Data.Level.Value = level
end)

If you want a more complex function to determine player's profit values, you can experiment with the functions listed here:

https://developer.roblox.com/articles/Lua-Libraries/math

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

For this specific example, let's say we have a starting max profit of 100, increasing by 10 upon reaching level 2, then 9, then 8, and so on.

That means that your max profit increases until the level transition from 10 to 11, where it will increase by 1 a final time.

There are a number of ways to do this, but off the top of my head, every time you level, simply increase the max profit by doing maxProfit = maxProfit + 12 - level as it is a linear decrease. That means, when you reach level 2, max profit will increase by 12 - 2, which is 10. When you finally reach level 11, then max profit increases by 12 - 11, which is 1.

Note that you'd have to figure out a way to stop adding once you reach level 11, or it'll start to reduce max profit. One way to do that would be to test if 12 - level is negative.

I'm not claiming at all that this is the best solution to this problem (it's almost guaranteed to not be), but rather how many different ways one can approach it.

Answer this question