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