Attempt to index nil with position

  1. What do you want to achieve?
    for my code to work
  2. What is the issue?
    it generates like over 100 or so trees then it just stops and says “attempt to index nil with position” on line 23
  3. What solutions have you tried so far?
    no idea how to fix it
local trees = {
	treeam = 1000,
	formatthing = "Tree",
	tree = game.ServerStorage.Tree,
	mapsize1X = game.Workspace.MapSize.MapSize1.Position.X,
	mapsize2X = game.Workspace.MapSize.MapSize2.Position.X,
	mapsize1z = game.Workspace.MapSize.MapSize1.Position.Z,
	mapsize2z = game.Workspace.MapSize.MapSize2.Position.Z
}

function TreeCode(tree)
	local number = math.random(10000,90000)
	local format = trees.formatthing.."#"..number
	local stringvalue = Instance.new("StringValue", tree)
	stringvalue.Value = format
end

function raycast(tree)
	local rayOrigin = tree.Position - Vector3.new(0,20,0)
	local rayDirection = Vector3.new(0, -500, 0)
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
	print(#game.Workspace.Tree:GetChildren())
	tree.Position = raycastResult.Position + Vector3.new(0,tree.Size.Y/2,0)
	TreeCode(tree)
end

function generate(x,z)
	local tree = trees.tree:Clone()
	tree.Parent = workspace.Tree
	tree.Position = Vector3.new(x,100,z)
	raycast(tree)
end
wait(1)
for I=1, trees.treeam do
	wait(0.000001)
	local x = math.random(trees.mapsize2X,trees.mapsize1X)
	local z = math.random(trees.mapsize2z,trees.mapsize1z)
	generate(x,z)
end

Clearly, the raycast isn’t hitting anything, causing the issue.

I have modified your code in such a way that it should no longer be an issue

local trees = {
	treeam = 1000,
	formatthing = "Tree",
	tree = game.ServerStorage.Tree,
	mapsize1X = game.Workspace.MapSize.MapSize1.Position.X,
	mapsize2X = game.Workspace.MapSize.MapSize2.Position.X,
	mapsize1z = game.Workspace.MapSize.MapSize1.Position.Z,
	mapsize2z = game.Workspace.MapSize.MapSize2.Position.Z
}

function TreeCode(tree)
	local number = math.random(10000,90000)
	local format = trees.formatthing.."#"..number
	local stringvalue = Instance.new("StringValue", tree)
	stringvalue.Value = format
end

function raycast(tree)
	local rayOrigin = tree.Position - Vector3.new(0,20,0)
	local rayDirection = Vector3.new(0, -500, 0)
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
    if not raycastResult then return end
	print(#game.Workspace.Tree:GetChildren())
	tree.Position = raycastResult.Position + Vector3.new(0,tree.Size.Y/2,0)
	TreeCode(tree)
    return true
end

function generate(x,z)
    local Result  = false
	local tree = trees.tree:Clone()
	tree.Parent = workspace.Tree
    repeat
        local x = math.random(trees.mapsize2X,trees.mapsize1X)
	    local z = math.random(trees.mapsize2z,trees.mapsize1z)
	    tree.Position = Vector3.new(x,100,z)
	    Result = raycast(tree) 
    until Result
end
wait(1)
for i=1, trees.treeam do
	task.wait()
	generate(x,z)
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.