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

Can someone help me fix this error: "Attempt to index a nil value"?

Asked by 5 years ago

I have a 3D Perlin noise generator, and I decided to make a script that will generate 3D perlin noise around the player based of where they walk. But I encountered an error that I can't fix. At line 53 it errors, And tells me "Attempt to index a nil value". Here is the script:

local chunks = {}

local function chunkExists(chunkX, chunkZ, chunkY) -- Check's if a chunk exists
        spawn(function()
    local isNil = false

  -- if not chunks[chunkX] then
  --    chunks[chunkX] = {}

  -- end
   if not chunks[chunkX] then
       chunks[chunkX] = {}
     --  return false
   end
   if not chunks[chunkX][chunkZ] then
       chunks[chunkX][chunkZ] = {}
     --  return false
   end
   if not chunks[chunkX][chunkZ][chunkY] then
       chunks[chunkX][chunkZ][chunkY] = {}
       --return false
   end

  -- if isNil == false then
       return chunks[chunkX][chunkZ][chunkY]
  -- end
    end)
end

local function roundTo(number, roundToNumber) -- Give it a value and it'll round it to the target's multiple, e.g 12 goes to 8, 17 goes to 16.
    return number - (number % roundToNumber)
end

local function CheckPos(X, Z, Y)
    spawn(function() -- We use spawn function because if we don't then every time this function is called then the (all) the client(s) that are running the game will freeze
        local RoundedX = roundTo(X, 32)
        local RoundedZ = roundTo(Z, 32)
        local RoundedY = roundTo(Y, 32)

        --print(" X: "..RoundedX.." Z: "..RoundedZ.." Y: "..RoundedY)
        --print(RoundedZ)
        --print(RoundedY)

        for x = -32, 32 do
            for z = -32, 32 do
                for y = -32, 32 do
                    local cx = RoundedX + x
                    local cz = RoundedZ + z
                    local cy = RoundedY + y

                    if not chunkExists(cx, cz, cy) == true then
                        --print("x: "..cx.." z: "..cz.." y: "..cy)
                        chunks[cx][cz][cy] = true

                    end
                end
            end
        end
    end)
end

spawn(function()
    while true do
        wait(1)
        --print("hh")
        for i, v in pairs(game:GetService("Players"):GetChildren()) do
            local C = nil
            local HRP = nil

            pcall(function()
                if v.Character then
                    C = v.Character
                else
                    C = nil
                end
            end)

            if C ~= nil then
                pcall(function()
                    if C:FindFirstChild("HumanoidRootPart") then
                        HRP = C:FindFirstChild("HumanoidRootPart")

                    else
                        HRP = nil
                    end
                end)
            end

            if HRP ~= nil then
                CheckPos(HRP.Position.X, HRP.Position.Z, HRP.Position.Y)
            end
        end
    end
end)

1 answer

Log in to vote
0
Answered by 5 years ago

I am still having this issue can someone PLEASE help me?

Ad

Answer this question