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

How do we use math.random?

Asked by 4 years ago

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?!??!?!

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. Rinpix 639 — 4y
0
Just read the wiki , its a simple thing. its one of the easiest thing talking about math in lua. EliteEngineer_DEV 0 — 4y
0
Show us the code where you are spawning yourself greatneil80 2647 — 4y

5 answers

Log in to vote
2
Answered by
Rinpix 639 Moderation Voter
4 years ago
Edited 4 years ago

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)
0
I did it but now It just puts me randomly in the baseplate not in the position given. InspireWithFun 9 — 4y
0
I mean, isn't that what we're trying to do here? Randomly position the spawn? Rinpix 639 — 4y
0
facepalm moments greatneil80 2647 — 4y
0
^ Agreed. Rinpix 639 — 4y
View all comments (2 more)
0
lol ur supposed to use vector 3 for positions coolmanHDMI 72 — 4y
0
^ You do realize that he later used the variables in a Vector3 value, correct? Rinpix 639 — 4y
Ad
Log in to vote
2
Answered by
zomspi 541 Moderation Voter
4 years ago

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!

1
huzzah, a fellow random.new() user Gojinhan 353 — 4y
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. Gojinhan 353 — 4y
1
A just straight to the point answer, nice, and you used up to date functions upvote for that marine5575 359 — 4y
Log in to vote
1
Answered by 4 years ago

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

0
I'm not that good at scripting, can you help? InspireWithFun 9 — 4y
Log in to vote
1
Answered by 4 years ago

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..?

0
It works but it doesn't spawns me at the given position. InspireWithFun 9 — 4y
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. Rinpix 639 — 4y
Log in to vote
-1
Answered by 4 years ago
Edited 4 years ago

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

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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
View all comments (38 more)
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. maxpax2009 340 — 4y
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. .. maxpax2009 340 — 4y
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. maxpax2009 340 — 4y
0
Fourth question; there were some nice guys who up-voted the others answers.... wait a second... yes that was me. maxpax2009 340 — 4y
0
What you learned from this; never judge a book by it's cover. Geesh. maxpax2009 340 — 4y
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. maxpax2009 340 — 4y
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. maxpax2009 340 — 4y
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. maxpax2009 340 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
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 maxpax2009 340 — 4y
0
I fixed the vector problem and when you spawn it creates a new spawn. maxpax2009 340 — 4y
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 maxpax2009 340 — 4y
0
local RandomNumber = math.random(23, 50) Print(RandomNumber) now here it will print a random number between these integers. maxpax2009 340 — 4y
0
the output would say a random number. maxpax2009 340 — 4y
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? maxpax2009 340 — 4y
0
W E M A K E A W H I L E L O O P maxpax2009 340 — 4y
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. maxpax2009 340 — 4y
0
And if there are more than the following 10 spawns it would break. maxpax2009 340 — 4y
0
It would do all that approximately under 0.03 seconds. maxpax2009 340 — 4y
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. maxpax2009 340 — 4y
0
So in a nutshell the statements about my personality were true, but the statements about my code were false. maxpax2009 340 — 4y
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. maxpax2009 340 — 4y
0
Just keep in mind I'm not naive. maxpax2009 340 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
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 maxpax2009 340 — 4y
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
1
Both of our answers were extremely good, but we had a different concept, i thought he wanted more than one spawn. maxpax2009 340 — 4y
1
I'm glad we've built common ground. I'll upvote your answer :) Rinpix 639 — 4y
0
what the heck greatneil80 2647 — 4y

Answer this question