NOTE: Use math.clamp() instead (thanks iuclds!)
This could probably be accomplished with a simple if statement or two to cap the maximum amount of prize money right after you set it. For example, you can make it so the prize money will only increase when it's less than or equal to a certain value. Here's what that would look like in practice, assuming you wanted to cap the value at 10000.
if money.Value < 10000 then
money.Value = money.Value +1000
end
if money.Value >10000 then
money.Value = 10000
end
Edit 1: Huh, never even know math.clamp existed. Thanks, iuclds! Use that answer instead.
Edit 2:
So it looks like you want to distribute the money across the players, giving the people who did better in the race more money. This could be applied a number of ways, and the answer that will be helpful for you will depend on you you attempt to implement it. I can't tell exactly what your project looks like, but here is an example I came up with.
Suppose you have an array of players called raceResults that records the results of the race. Maybe it stores who came in first place first, and second place second, etc. You could read through the list, and give each player a portion of the prize money. For example, let's say there are 4 players, and $10 to distribute: you can give the first person the highest percentage of the money, and the last person the lowest percentage of the money. What could this look like? For demonstration purposes, instead of giving players money I'm going to print out what they would get:
local raceResults = {"Spiderman", "Batman", "Superman", "Hulk"}
local playerNumber = 4
local payout
local money = 10
for index, value in ipairs(raceResults) do
payout = (playerNumber - index) / 3
print(value, "recieves", math.floor(payout * money))
end
Output:
Spiderman recieves 7
Batman recieves 5
Superman recieves 2
Hulk recieves 0
So what's going on here? Firstly, I'm using preset values for raceResults, playerNumber, and money. In your game, these will be determined by factors in the game, like how many players there actually are. Now, we increment through this array using a for loop. index means which player in the list are we on each time we go through the loop (1, 2, 3, or 4) and value is what is there in the array (the names of the superheroes). Then, I calculate their payout by subtracting their place in the race from the total number of players and dividing that by the number of players. This will create a decimal between 0 and 1 that can be used to calculate how much money they get. Since there are four players, the person in first place gets ((4 - 1) / 4) or 2/3 of total money available. Next, I do the final calculation, where we just give the player the payout * money (or for the person in first place, 3/4 of the $10. math.floor just drops the decimal places. One thing to note, however, is that this doesn't treat the money.Value (money in the example) as a pool of money, but just a value that is used to calculate payouts.
If this is confusing, don't give up! I encourage you to do research to find a method that works for you, but if you adapt the code I gave above properly, it can work! One thing also to note is that in my array I have the players as strings, which may be actual players for you. If you have any questions, feel free to leave another comment or send me a message on ROBLOX!
10 comments
ok, thats not really what im asking for but it does help, so i will be accepting the answer! Thanks anyway!
Oh, no problem! Do you want to try to explain again what you meant so I can try to help you better?
Ok, so if youve played drving simulator youlll know that the amount of players in a race varys, as in other games. but hile that amount of players vary, so does the 1st place prize money. if 10/10 players are in the race then the 1st place gets like 6.3k. if fewer then the 1st place gets lower. how would i code that into my game? like if lets say 20 players are in the race, then the 1 st place
Ok, so if youve played drving simulator youlll know that the amount of players in a race varys, as in other games. but hile that amount of players vary, so does the 1st place prize money. if 10/10 players are in the race then the 1st place gets like 6.3k. if fewer then the 1st place gets lower. how would i code that into my game? like if lets say 20 players are in the race, then the 1 st place
gets x amount of money and 2nd place gets a little lower and so on. Thanks for your help!
SOrry I cant accept two answers at one time, if you provide th answer said before then I will probably accept. Thanks!
I just added to my answer. Have a look!
Thanks! That makes it a lot more clear to me. I will try to adapt that into my game!
Ok! Just go to my profile and send me a message through Roblox if you need any more help :)
thanks!