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 6 years ago
Edited 6 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 6 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)

01local player = game.Players.LocalPlayer
02local Data = player:WaitForChild("Data")
03 
04local level = Data:WaitForChild("Level")
05local profit = Data:WaitForChild("Profit")
06 
07local MaxProfit = 100
08local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
09 
10level.Changed:Connect(function()
11local Level = level.Value
12local Profit = profit.Value
13    if Profit < MaxProfit then
14        if Level <= 10 then
15            local create = math.abs(11 - Level)
View all 21 lines...

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)

01local DSS = game:GetService("DataStoreService")
02local Storage = DSS:GetDataStore("Data")
03local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
04 
05game.Players.PlayerAdded:Connect(function(player)
06    local LoadArray = Storage:GetAsync(player.UserId)
07 
08    if LoadArray ~= nil then
09        local datafolder = Instance.new("Folder", player)
10        datafolder.Name = "Data"
11 
12        local level = Instance.new("IntValue", datafolder)
13        level.Name = "Level"
14        level.Value = LoadArray[1]
15 
View all 42 lines...

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 6 years ago
Edited 6 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