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

How do we use math.random?

I'm making a game called tower of clone, inspired by Tower of hell, So What i'm doing is, putting the spawn point at a random position using math.random, but it wont work, Im making it like:

local spawnpoint = game.Workspace.MainSpawn

local xPos = math.random(-12.96 , 217.21)
local Height = 41
local zPos = math.random(71.05 , -46.1)

spawnpoint.Position = Vector3.new(xPos,Height,zPos)

But this doesn't work, please help me, how do I change the spawn locations position to the random number?!??!?!

3 comments

0

When specifying the range in which you want the randomly selected number to be, I would put the lower number first. On line 5, you put the lower number second. Switch them and see if it works.

0

Just read the wiki , its a simple thing. its one of the easiest thing talking about math in lua.

0

Show us the code where you are spawning yourself

5 answers

When using math.random, you're supposed to specify the lower number first, and not the higher number.

On line 5, you specified the lower end of the range second, when you were supposed to do it first.

local zPos = math.random(71.05 , -46.1)

It should look like this:

local zPos = math.random(-46.1 , 71.05)

6 comments

0

I did it but now It just puts me randomly in the baseplate not in the position given.

0

I mean, isn't that what we're trying to do here? Randomly position the spawn?

0

facepalm moments

0

^ Agreed.

0

lol ur supposed to use vector 3 for positions

0

^ You do realize that he later used the variables in a Vector3 value, correct?

There are a few issues, first of all you should never use math.random as it isn't actually random, it gets the next number off of the previous result. Instead use: Random.new(tick()):NextNumber()

You script errors because the first number is greater than the second, this is a fixed version of your script:

local spawnpoint = game.Workspace.MainSpawn

local xPos = Random.new(tick():NextNumber(-12.96, 217.21)
local Height = 41
local zPos = Random.new(tick():NextNumber(46.1, 71.05)

spawnpoint.Position = Vector3.new(xPos,Height,zPos)

NextNumber is used for decimal numbers NextInteger is used for whole numbers

If I helped please accept this answer!

3 comments

1

huzzah, a fellow random.new() user

1

oh just thought i'd inform you, math.random and random.new use the same random algorithm as of late 2017-ish. That said random.new is still better due to being able to choose between int and numbers.

1

A just straight to the point answer, nice, and you used up to date functions upvote for that

I would recommend just checking for when the player respawns and teleporting them to the random position after they spawn by setting Cframe primary part

1 comment

0

I'm not that good at scripting, can you help?

Try this perhaps.

local spawnpoint = game.Workspace.MainSpawn

local xPos = math.random(-1296 , 21721)/100
local Height = 41
local zPos = math.random(-4610,7105)/100

spawnpoint.Position = Vector3.new(xPos,Height,zPos)

I swapped out the decimals so that its whole numbers, divide it by 100 so you get decimals. I also swapped your negative and positive...

Feel free to tell me if it doesn't work.

I did it but now It just puts me randomly in the baseplate not in the position given.

wdym puts "YOU", what about the spawnpoint..?

2 comments

0

It works but it doesn't spawns me at the given position.

0

I think it's because you're spawning at the initial position of the SpawnLocation, the spawn is then randomly positioned, and there you have it. I'd just create a part and as soon as the player's character loads, move it to the spawn by calling :MoveTo() on the character and providing the part's position. If you're already going with a part, you have to tell the script to move the player to it.

Roblox LUA does not understand what you want to do.

It's going to generate the same random number every time.

I'd also recommend putting the spawn in ReplicatedStorage so we can clone it. And also create a folder in workspace called spawns where we parent the spawns later on

So put it in a while loop like this;

local MainSpawn = game.Workspace.MainSpawn:Clone()

local AllSpawns = game.Workspace.Folder

function generate()
	local PosX = math.random(-12.96 , 217.21)
	
	local PosZ = math.random(71.05 , -46.1)
	
	local NewVector = Vector3.new(PosX, 41, PosZ)
	
	return NewVector
end

while true do
	wait()
	local PartVector = generate()
	
	MainSpawn.Position = PartVector
	MainSpawn.Parent = game.Workspace.Folder
	
	if	#AllSpawns:GetChildren() > 10 then-- the max number of spawns you want
        break
	end
	end

The things you need to learn;

Returning; basically stops the function like a break in loops. And you can define the variable too.

# is a number for something that isn't a numbervalue:

Here's an example

function checkplayers()
	if #game.Players:GetPlayers() < 2 then
		print("2 or more player required to play the game")
	end
end

while wait(2) do
	checkplayers()
end

42 comments

0

Nice job downvoting everyone else's perfectly legitimate response. Regardless, this response is overly convoluted for such a simple problem. Typically I wouldn't criticize someone's answer, but since you're reputation farming, well uh I can't restrain myself.

0

There are two problems: he put the higher value of the range on line 5 first, and the player is spawning at the spawn's initial position, and then the spawn is being moved. Shouldn't make for such a complicated solution.

0

math.random doesn't generate the same number every time. I don't know what context you mean, but the same sequence of numbers isn't generated from server to server, and the same number isn't generated from iteration to iteration. You also put the negative number second in the math.random function.

0

Rushing a response and down voting all of the other ones is just being dirty. If I'm going down, you're going down with me. And thankfully, two people decided to upvote the downvoted responses.

0

I actually up-voted them for the sake of not arguing with this 'Rinpix', guy. Your first question was... :  that it was to messy... It's not messy at all, this is how you do it.

0

Second question; when you spawn then another spawn spawns; Good you said that, because now i added a wait() so it's going to wait a matter of 0.03 seconds before another spawn spawns. ..

0

Third question; i messed up with the vectors... He can always change them, i just messed up a little bit. He can see that he needs to adjust the vectors alignment.

0

Fourth question; there were some nice guys who up-voted the others answers.... wait a second... yes that was me.

0

What you learned from this; never judge a book by it's cover. Geesh.

0

And no It's not over-convoluted or repetitive. you put it in a function so we can run it multiple times. If we did not do that it would generate the same random number every time.

0

Also if i read this question legitimately i can surely see he asked for more than one spawn to be in a random position.

0

Learn a bit more basic LUA, then come back to me and argue about it. Also while you're there look up; 'math.random', and see that if you just generate a random number without a loop it will generate the same number.

0

You downvoted them to begin with. When I pointed it out, you upvoted them. So after realizing that someone criticized you for a particular action, you reverted that action. I never claimed it was messy, I just claimed it was too convoluted for the question that he asked.

0

You misplaced the two values in the math.random() function. It takes a NumberRange value, which requires that the first value be less than or equal to the second value. Indeed, you did upvote the other answers, but only after I pointed out you downvoted them.

0

I didn't judge a book by its cover. I judged a book by its contents. Your intentions were purely to farm reputation points. You downvoted other responses to get yours to the top. And why wasn't yours upvoted if someone else upvoted them? Because you can't upvote your own answers.

0

And yes, it absolutely is overly convoluted, for how simple of a problem the original poster encountered was. And, no, it wouldn't generate the same number every single time. I even tested it before I said that it wouldn't, because I don't say things with absolute certainty without first confirming it. He didn't say anywhere in the question that he was going to do this multiple times.

0

I've been learning Lua since January 2015, and I'm not exactly an expert, nor do I purport to be, but I'm definitely not a beginner. There's nothing to argue about here. Your response was rushed and you downvoted everyone else's answer and then upvoted them when I pointed it out. I have been using math.random forever, and I can assure you that it does not generate the same number every time.

0

And before you cite my join date as evidence that I'm inexperienced, I originally joined in 2011. This is just an account I've created over the span of 9 years.

0

'Judging a book by it's contents', see man. You literally just saw me today. Yes i had an intention of farming since I'm bored in quarantine. But your statements about my code are completely out of balance

0

I fixed the vector problem and when you spawn it creates a new spawn.

0

Now it's your statement about over-convoluted code. I got this info from the wiki, it legitimately says that if you generate a random number once it will generate the same number again

0

local RandomNumber = math.random(23, 50) Print(RandomNumber) now here it will print a random number between these integers.

0

the output would say a random number.

0

If i run the code again and join it will print a different number. Now the problem here is that he wants to one spawn that replicates itself and puts them in random positions so what do we do?

0

W E M A K E A W H I L E L O O P

0

Yes, we make a while loop. So it generates random numbers under the tick; 0.03 that would occur even before the player spawns, then it would put them all in random positions.

0

And if there are more than the following 10 spawns it would break.

0

It would do all that approximately under 0.03 seconds.

0

And yes it was disruptive and wrong of me too down-vote the other answers for my own good. I have already learned that when you pointed it out.

0

So in a nutshell the statements about my personality were true, but the statements about my code were false.

0

You also have no proof you started scripting in 2011 [9 years ago], I could also say to you without no proof; 'I survived titanic guys', majority would not believe that.

0

Just keep in mind I'm not naive.

0

I judged you on a basis of what you did and how you did it, not your username, your typing, or your avatar. I judged a book by its contents and not by its cover. My statements aren't completely out of balance. All I'm saying is you've provided way more than was requested.

0

By "convoluted", I mean that your code is excessive. It is much more complicated than it needs to be, given how simplistic the issue the original poster encountered is. The Wiki is unreliable; it's ran by people that don't work at Roblox, and it's not routinely kept up to date. I also checked the Wiki, and it doesn't say anything about generating the same number over and over again.

0

Again, I tested it. Math.random doesn't give you the same number over and over again. The page you got it from is outdated. I believe it used to give you the same number over and over again, but it certainly doesn't. I think you're misconstruing the fact that the same seed produces the same sequence of randomly generated numbers, which is true. But without generating a seed, it's totally random.

0

He never explicitly stated that he wanted to do this multiple times. That's irrefutable and not worth arguing about. Adding a wait pauses the script. It would not do it in UNDER approximately 0.03 seconds, because it would wait 0.03 seconds, and then perform the actions, which is already not under 0.03 seconds. In addition, it does take some time to execute the following code, but it's negligible.

0

Ohhhh, i thought he wanted to have more than one spawn... now i get it. I thought he wanted to have more than one spawn. i get i provided more, but if he wanted to have more than one spawn then my code would be useful

0

Both the statements about your personality and of your code were true. Of course I don't have any proof that I started learning Lua in 2011, because I didn't. I said I originally joined in 2011 and that I started to learn in January 2015. I did initially join in 2011 and could message you on one of my accounts that I created in that year if you'd like.

0

Well, I'm not saying that he didn't want to do this multiple times, I'm just saying that he didn't explicitly state it.

1

Both of our answers were extremely good, but we had a different concept, i thought he wanted more than one spawn.

1

I'm glad we've built common ground. I'll upvote your answer :)

0

what the heck

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