Ello, so I'm making a very bare bones race track (just checkpoints,) but I've get the check points sorted out and all. But how would I make the start? ofc I know how to make the start (very bare bones start, you'll see) but when a player hits it and moves around, the number of players int value goes up. so does the first place money prize im also calculating (like driving simulator, if you've played it, but i'm not putting a limit on the number of people in the race, more people = more playtime = more people grinding = more premium playtime score = STONKS) I don't want first place players getting like 5 mill when they win, for obvious reasons. So how would I do that? Heres the code:
local money = script.Parent.firstplace local players = script.Parent.numofplayers local Part = script.Parent local function PlayerTouched(Part) players.Value = players.Value + 1 money.Value = money.Value +1000 end Part.Touched:connect(PlayerTouched)
now of course theres an obvious problem, before mentioned. What funtion should I use? Or should the whole code be scrapped and start out with a new one? Please help, thanks!
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 -- Only add money if we don't already have 10000 end if money.Value >10000 then money.Value = 10000 -- Set the money back to 10000 if it goes over 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!
math.clamp(Value,Minimum,Maximum)
math.clamp is much faster and much much more efficient than the answer above.
if Value < Minimum then it returns Minimum
if Value > Maximum then it returns Maximum
else if Value < Maximum and Value > Minimum then it returns Value
Here's an example.
print(math.clamp(5,0,4)) -- 4 print(math.clamp(5,0,10)) -- 5 print(math.clamp(-5,3,4)) -- 3