Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Have I used math.random() right?

I am making a tower defence game and it was previously a 1 man server so I'm making it so multiple people can play in the same server but I need to give players there own numbers. Basically I want every player to have a different number, but ima make it so there is only 4 numbers. So I can address each player.

game.Players.PlayerAdded:connect(function(p)
	local stats = Instance.new("IntValue")
	stats.Name = "team2"
	stats.Parent = p
	
	local money = Instance.new("IntValue")
	money.Name = "which" 
	money.Value = 1 
	money.Parent = stats
	
	p.team2.which.Value = math.random(1,4)
end)

1 comment

0

You are using it correctly, but there's one issue. There's the possbility of the teams value to be the same as the other one, since there's only a 25% chance to have duplicates.

2 answers

✓ Accepted answer

You are using math.random right, but since you want to give each player a different number, we need to use a table to keep track of the possible numbers:

local possibleNumbers = {1,2,3,4} --this is a list of possible numbers that the player can be given

--pick a random number from the list of possible numbers
local randomIndex= math.random(1, #possibleNumbers)
local chosenNumber = possibleNumbers[randomIndex]

--remove that number from the possibleNumbers list so the number can't be picked again
table.remove(possibleNumbers, randomIndex)

--assign the player the chosen number
p.team2.which.Value = chosenNumber 

2 comments

0

Thx soo much

1

np

First of all, let's go over the correct use of math.random(). To get a basic random number, you would use this:

local randomNumber = math.random(1,10) --You did this correctly

You could generate random seeds for better random numbers. I will not explain those here, but you could look into them later.

From what I can understand, you are trying to seperate players into teams. However, let's go through your program by saying four people join. Let's say these are the following numbers that generated:

1,3,1,1

Team 3 would greatly outnumber Team 1. You would want to make an array of the possible teams to get on:

local teams = {1,2,3,4}

Now, we select a team from this table by using math.random():

local index = math.random(1,#teams) --Pick a position in the table
local team = teams[index] --Now we reference the team inside the table

Then set their team to the number:

p.team2.which.Value = team --I assume you want a number value

Then remove this from the table so it cannot be chosen again:

table.remove(teams,index) --This is why we got an index instead of just getting the team.

Hope this helped!

2 comments

0

thx

0

No problem. Just keep in mind that if you wanted to get an even amount of people, you would want to use math.floor() on the number of players divided by the number of teams

This discussion is preserved as it was. New answers, comments, and votes are closed.