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

Math.Random for vector3 positions, How to use?

--[[
Settings for terrian generation
]]

local Maxheight = 10
local Minheight = -10

--[[
Varibles
]]

local GenerationInProgress = false
local TerrianGenerationFinished = false
local IsChunkLoading = false

local TerrianFolder = workspace.TerrianF
local Chunks = TerrianFolder.Chunks


function StartGeneration()
	if GenerationInProgress == false and TerrianGenerationFinished == false then
		GenerationInProgress = true
		LoadChunk()
		warn("Started Loading Chunk")
	else
		warn("Error Code 0/01")
	end
end

function LoadChunk()
	if IsChunkLoading == false then
		ChunkLoop()
	else
		warn("Error Code 0/02")
	end
end

function ChunkLoop()
	for i,v in pairs(Chunks:GetChildren()) do
		if v.IsLoaded.Value == false then 
			IsChunkLoading = true
			NodeLoop(v)
		else
			warn("Error 0/03")
		end
	end
end

function NodeLoop(v)
	for i,n in pairs(v.Nodes:GetChildren()) do
		n.Position = Vector3.new(0,math.random(Maxheight,Minheight),0)
		warn("Set Nodes i guess?")
	end
end

StartGeneration()

My error is ServerScriptService.Script:51: invalid argument #2 to 'random' (interval is empty) witch is in the nodeloop function

1 comment

0

hey math.random(minheight,maxheight) not math.random(maxheight,minheight) Remember thank, goodbye.

2 answers

Remember that Vector3 only takes 3 arguments, no more, of X, Y, Z. One of the intervals you added was empty, which were the ones outside the enclosed brackets.

##Here are the fixed script(s):

--[[
Settings for terrain generation
]]

local Maxheight = math.random(10)
local Minheight =math.random(-10)


--[[
Variables
]]

local GenerationInProgress = false
local TerrianGenerationFinished = false
local IsChunkLoading = false

local TerrianFolder = workspace.TerrianF
local Chunks = TerrianFolder.Chunks


function StartGeneration()
	if GenerationInProgress == false and TerrianGenerationFinished == false then
		GenerationInProgress = true
		LoadChunk()
		warn("Started Loading Chunk")
	else
		warn("Error Code 0/01")
	end
end

function LoadChunk()
	if IsChunkLoading == false then
		ChunkLoop()
	else
		warn("Error Code 0/02")
	end
end

function ChunkLoop()
	for i,v in pairs(Chunks:GetChildren()) do
		if v.IsLoaded.Value == false then 
			IsChunkLoading = true
			NodeLoop(v)
		else
			warn("Error 0/03")
		end
	end
end

function NodeLoop(v)
	for i,n in pairs(v.Nodes:GetChildren()) do
		n.Position = Vector3.new(math.random(Maxheight,Minheight))
		warn("Set Nodes i guess?")
	end
end

StartGeneration()

OR you could do this:

--[[
Settings for terrain generation
]]


--[[
Variables
]]

local GenerationInProgress = false
local TerrianGenerationFinished = false
local IsChunkLoading = false

local TerrianFolder = workspace.TerrianF
local Chunks = TerrianFolder.Chunks


function StartGeneration()
	if GenerationInProgress == false and TerrianGenerationFinished == false then
		GenerationInProgress = true
		LoadChunk()
		warn("Started Loading Chunk")
	else
		warn("Error Code 0/01")
	end
end

function LoadChunk()
	if IsChunkLoading == false then
		ChunkLoop()
	else
		warn("Error Code 0/02")
	end
end

function ChunkLoop()
	for i,v in pairs(Chunks:GetChildren()) do
		if v.IsLoaded.Value == false then 
			IsChunkLoading = true
			NodeLoop(v)
		else
			warn("Error 0/03")
		end
	end
end

function NodeLoop(v)
	for i,n in pairs(v.Nodes:GetChildren()) do
		n.Position = Vector3.new(math.random(10,-10))-- your positions
		warn("Set Nodes i guess?")
	end
end

StartGeneration()

####Edit: Fixed grammatical errors!

going off the principal that Vector3.new has the parameters in order of Vector3.new(X,Y,Z), we can use this:

Vector3.new(0,math.random(minheight,maxheight),0)

to create a new random vector between minheight and maxheight on the Y axis (up/down).

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