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:
1 | local spawnpoint = game.Workspace.MainSpawn |
2 |
3 | local xPos = math.random(- 12.96 , 217.21 ) |
4 | local Height = 41 |
5 | local zPos = math.random( 71.05 , - 46.1 ) |
6 |
7 | spawnpoint.Position = Vector 3. new(xPos,Height,zPos) |
But this doesn't work, please help me, how do I change the spawn locations position to the random number?!??!?!
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.
1 | local zPos = math.random( 71.05 , - 46.1 ) |
It should look like this:
1 | local zPos = math.random(- 46.1 , 71.05 ) |
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:
1 | local spawnpoint = game.Workspace.MainSpawn |
2 |
3 | local xPos = Random.new(tick():NextNumber(- 12.96 , 217.21 ) |
4 | local Height = 41 |
5 | local zPos = Random.new(tick():NextNumber( 46.1 , 71.05 ) |
6 |
7 | spawnpoint.Position = Vector 3. new(xPos,Height,zPos) |
NextNumber is used for decimal numbers NextInteger is used for whole numbers
If I helped please accept this answer!
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
Try this perhaps.
1 | local spawnpoint = game.Workspace.MainSpawn |
2 |
3 | local xPos = math.random(- 1296 , 21721 )/ 100 |
4 | local Height = 41 |
5 | local zPos = math.random(- 4610 , 7105 )/ 100 |
6 |
7 | spawnpoint.Position = Vector 3. 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..?
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;
01 | local MainSpawn = game.Workspace.MainSpawn:Clone() |
02 |
03 | local AllSpawns = game.Workspace.Folder |
04 |
05 | function generate() |
06 | local PosX = math.random(- 12.96 , 217.21 ) |
07 |
08 | local PosZ = math.random( 71.05 , - 46.1 ) |
09 |
10 | local NewVector = Vector 3. new(PosX, 41 , PosZ) |
11 |
12 | return NewVector |
13 | end |
14 |
15 | while true do |
The things you need to learn;
Returning; basically stops the function like a break in loops. And you can define the variable too.
Here's an example
1 | function checkplayers() |
2 | if #game.Players:GetPlayers() < 2 then |
3 | print ( "2 or more player required to play the game" ) |
4 | end |
5 | end |
6 |
7 | while wait( 2 ) do |
8 | checkplayers() |
9 | end |