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

Trying to get trees to spawn and all the parts generate, but the meshes only go to one!?

Asked by
Kennqu 3
4 years ago

So how the script works is It looks for terrain with rays and all that good stuff, summons a part randomly on the terrain, and sets the properties. The problem is all 2000 parts generate, but the 2000 meshes only go to one part instead of all 2000 parts. My code here:

--Tree Gen, Codded by Kennqu

math.randomseed(tick()) --Will 'randomize' our pseudo-random num generator.
local check_height = 100 --Makes sure trees wont spawn Over the designated Y axis pos.
local map_x, map_y = 4000, 4000 --Define map size.
local Num = 2000 --How many trees to be spawned.

--Now, lets use a loop to set how many trees will be placed.

for i = 1, Num do 
    local rand_x = math.random()*map_x
    local rand_y = math.random()*map_y

    local ray = Ray.new(
        Vector3.new(rand_x, check_height, rand_y),
        Vector3.new(0, -1, 0)*300
    )

    local inst, position = workspace:FindPartOnRay(ray, nil, false, true)

    if inst and inst:IsA("Terrain") then
        --(rand_x, position.y, rand_y)




        local NewTree = Instance.new("Part", workspace)
        NewTree.Position = Vector3.new(rand_x, position.y, rand_y)
        NewTree.Anchored = true
        NewTree.CanCollide = false
        NewTree.Transparency = 0.012
        NewTree.Name = ("TreeG")
    end

end


for i = 1, Num do --Gives the empty 'Invisible' parts a mesh to make it a tree.

    function GiveMesh()
        local NewTreeBody = Instance.new("SpecialMesh", workspace.TreeG)
        NewTreeBody.MeshId = ("rbxassetid://2640968479")
        NewTreeBody.TextureId = ("rbxassetid://2640968592")
        NewTreeBody.Offset = Vector3.new(0, 36, 0)
        NewTreeBody.Scale = Vector3.new(4, 8, 4)
    end

    GiveMesh()

end

1 answer

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago

when you create the tree, give it a mesh. right now you're picking a single tree (workspace.TreeG) and putting a mesh inside of that tree only i explained more in the comments

math.randomseed(tick()) --Will 'randomize' our pseudo-random num generator.
print(math.random()*4000)
local check_height = 100 --Makes sure trees wont spawn Over the designated Y axis pos.
local map_x, map_z = 4000, 4000 --Define map size.
local num_trees = 2000 --How many trees to be spawned.

--Now, lets use a loop to set how many trees will be placed.
for i = 1, num_trees do 
    --find a random position on the map to place tree
    local rand_x = math.random()*map_x
    local rand_z = math.random()*map_z
    local rayStartPosition = Vector3.new(rand_x, check_height, rand_z)

    --raycast downwards 300 studs from specified start position to find the height of the position we picked
    local ray = Ray.new(rayStartPosition, Vector3.new(0, -1, 0)*300)
    local instance, position = workspace:FindPartOnRay(ray, nil, false, true)

    --if the random position we found is terrain, then create a tree and give that tree a mesh
    if instance and instance:IsA("Terrain") then
        --create a tree
        local NewTree = Instance.new("Part")
        NewTree.Parent = workspace
        NewTree.Position = Vector3.new(rand_x, position.y, rand_z)
        NewTree.Anchored = true
        NewTree.CanCollide = false
        NewTree.Transparency = 0.012
        NewTree.Name = ("TreeG")

        --give that tree a mesh, putting the mesh inside the tree we just created
        local NewTreeBody = Instance.new("SpecialMesh")
        NewTreeBody.Parent = NewTree 
        NewTreeBody.MeshId = ("rbxassetid://2640968479")
        NewTreeBody.TextureId = ("rbxassetid://2640968592")
        NewTreeBody.Offset = Vector3.new(0, 36, 0)
        NewTreeBody.Scale = Vector3.new(4, 8, 4)
    end
end



0
Think you kind sir! Kennqu 3 — 4y
0
np pls ask questions if you have any questions royaltoe 5144 — 4y
0
he still hasnt accepted the answer yet speedyfox66 237 — 4y
0
Answer has been accepted. DeceptiveCaster 3761 — 4y
0
ty bruv <3 royaltoe 5144 — 4y
Ad

Answer this question