How could I pick a player from a percentage (with dev product)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to be able to pick someone to be it/siren head in my game but to increase your chances you can buy a developer product.

  2. What is the issue? Include screenshots / videos if possible!
    I can’t figure out how to do it.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked for solutions on the developer hub/forum, although they were either not working or not good in my situation.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category. (sorry if this is a big ask)

1 Like

Oh this is a simply one. Simply use a non pure function such as the random method and decrease the foci. Example -

local function codeToProcessIfPlayerIsChossen()
  return end
end
local hasGamePass = false
-- gamepass event
hasGamePass = true
-- siren head event
if hasGamePass then
  if math.random(0, 10) == 0 then
    codeToProcessIfPlayerIsChossen()
  end
  elseif not hasGamePass then
   if math.random(0, 100) == 0 then
    codeToProcessIfPlayerIsChossen()
  end
end
1 Like

If you’re doing it based on percentage then this is how you should think about it.

Let’s say you have 5 players and each player has a value of 5 for your percentage ‘wheel.’ In this scenario each player would have a 20% chance of being picked because there is a total of 25 in the value. Now let’s say one player decides to purchase a dev product that grants the 10 more points, now that player has a 42% chance of winning while everyone else has a 14% chance of winning because there is now a total of 35 points.

Now how do you tell your script to do this?
Well first you gather your particpants by using a table, use table.insert for the players.
Then you get a total of the points which you can get from each player’s leaderboard stats of the points they have and add them together.
Then to generate the random number you just use math.random(0, TotalNumber)
And once you have your randomly generated number, you go through each participant by setting an inital check value.
The inital check value will be 0 and you check if the generated number is greater than the inital check value and less than or equal to the inital check value plus the participant’s value. If they meet that statement, you have your winner, otherwise you continue down the list of partipants until you have your lucky winner.

Of course if you’re going to make it where players can pay real money to increase their chances, you should make the values of each player save and also avoid implementing real gambling into your game.

1 Like

Can you re-phrase this its really hard to understand but it seems very useful…

I’ll rephrase it and put some code examples/pictures to hopefully make it easier for you (and possibly others) to understand.

Imagine you have 5 players in your game and you only want one of them to be selected for something rather if it’s VIP, Leader, or whatever. One of the most simplistic ways of choosing someone out of that list is to use math.random from the player list, here’s an example.

local ListOfPlayers = game.Players:GetPlayers()
local ChosenPlayerPos = math.random(1, #ListOfPlayers)
local ChosenPlayer = ListOfPlayers[ChosenPlayerPos]

In the first line I gather the list of players that I know are participating to be chosen. Using :GetPlayers() returns a list of players that are connected into your game which looks like this:
image

Now in the second part I picked a position which I simply used math.random. When you use math.random using interger numbers for the first and second part (like math.random(1, 3)) it will return a random generated number between the first number and last number. By doing math.random(1, #ListOfPlayers) I am basically doing math.random(1, 5) which I am getting a random number between 1 and 5.

Now that I got my chosen random number I basically go back into the table and grab the player using my random number and retrieve the player that has been chosen. So if my random chosen number was number 3 this would return me Player 3.

image

This is a simple script that’s fine to use to pick a player with them all being completely equal percentage wise and all of them having no advantage over the other. In this scenario all players would have a 20% chance of being chosen because everyone would have a 1 / 5 chance of being chosen. If there was 10 players all players would have a 10% chance of being chosen because you would only have a 1 / 10 chance of being chosen.

Now if you want to give players an opportunity to purchase a dev product to increase their chance of being chosen for something, it’s necessary to use something not as simple as math.random. I suggest using a point value system. There are other ways to achieve this but I like this system and I hope you do too lol.

Imagine in the same scenario you have 5 players in your game that want to be chosen and now let’s say each player has 5 points. In this scenario you’re going to need to setup and do two tables for this. So here’s a scripting example

local TotalPlayerPoints = 0
local DefaultPlayerCount = 5
local ListOfPlayerPoints = {}
local ListOfPlayers = game.Players:GetPlayers()
for PlayerNum = 1, #ListOfPlayers do
	local Player = ListOfPlayers[PlayerNum]
	local CurrentPlayerPoints = DefaultPlayerCount
	table.insert(ListOfPlayerPoints, PlayerNum, CurrentPlayerPoints)
	TotalPlayerPoints = TotalPlayerPoints+CurrentPlayerPoints
end

Right now we’re setting up a list of participants into a point list which will look something like this.

If for example your dev product increased the chance points by +5, you would add CurrentPlayerPoints = DefaultPlayerCount+Dev(+5) to the script.

Now to get into the player choosing part you can use math.random but you’ll have to iterate your new participants of player points. We have the Total Points so now we just pick a random number and then we loop through all of the points to make sure one of the players are are in that range. So for scripting example

local WinnerPlayerPoint = TotalPlayerPoints*math.random() --25 total if 5 players
local MinPointRoll = 0
local ChosenPlayerPos = 0 --Not chosen yet
for PlayerPointNum = 1, #ListOfPlayerPoints do
	local MaxPointPoll = MinPointRoll+ListOfPlayerPoints [PlayerPointNum]
	if WinnerPlayerPoint >= MinPointRoll and WinnerPlayerPoint <= MaxPointPoll then
		ChosenPlayerPos = PlayerPointNum
		--We have a winner!
		break
	else
		--We don't have a winner, keep drawing
		MinPointRoll = MinPointRoll+MaxPointPoll
	end
end

And basically what’s going on in your script is that it’s just going through the points to make sure the chosen number is in the range of the points of your participants with player number 4 being the winner.

Hope this helped!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.