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