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

How would I not make people getting like 5mil in a race?

Asked by 4 years ago

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!

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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!

0
ok, thats not really what im asking for but it does help, so i will be accepting the answer! Thanks anyway! Spiyder1 81 — 4y
0
Oh, no problem! Do you want to try to explain again what you meant so I can try to help you better? MrBlockyhead 84 — 4y
0
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 Spiyder1 81 — 4y
0
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 Spiyder1 81 — 4y
View all comments (6 more)
0
gets x amount of money and 2nd place gets a little lower and so on. Thanks for your help! Spiyder1 81 — 4y
0
SOrry I cant accept two answers at one time, if you provide th answer said before then I will probably accept. Thanks! Spiyder1 81 — 4y
0
I just added to my answer. Have a look! MrBlockyhead 84 — 4y
0
Thanks! That makes it a lot more clear to me. I will try to adapt that into my game! Spiyder1 81 — 4y
0
Ok! Just go to my profile and send me a message through Roblox if you need any more help :) MrBlockyhead 84 — 4y
0
thanks! Spiyder1 81 — 4y
Ad
Log in to vote
2
Answered by
iuclds 720 Moderation Voter
4 years ago
Edited 4 years ago

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
0
ok, so ive been reading about math.clamp because I have no idea what it is. I couldnt find anything on devhub ( https://developer.roblox.com/en-us/ ) but i didnt find something on the dev fourm ( https://devforum.roblox.com/t/math-clamp-explanation/285376/2 ) I will try it though, thanks anyway! Spiyder1 81 — 4y
0
*did Spiyder1 81 — 4y

Answer this question