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

Math.Random for vector3 positions, How to use?

Asked by
hokyboy 270 Moderation Voter
2 years ago
--[[
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

0
hey math.random(minheight,maxheight) not math.random(maxheight,minheight) Remember thank, goodbye. lehoaiquoc248 23 — 2y

2 answers

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

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!

Ad
Log in to vote
0
Answered by 2 years ago

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

Answer this question