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

attempt to index nil with 'Name'?

Asked by 1 year ago

Hello everybody, i'm trying to make a DOORS game and i got an error trying to add in probability of generation.

  18:59:40.323  ServerScriptService.Server.Generation:24: attempt to index nil with 'Name'  -  Server - Generation:24
  18:59:40.323  Stack Begin  -  Studio
  18:59:40.323  Script 'ServerScriptService.Server.Generation', Line 24 - function getRandom  -  Studio - Generation:24
  18:59:40.323  Script 'ServerScriptService.Server.Generation', Line 40 - function generate  -  Studio - Generation:40
  18:59:40.323  Script 'ServerScriptService.Server', Line 6

But, this error has to do something with the gen.getRandom() function.

Here is the full code:

local gen = {}
gen.random = Random.new()
gen.info = require(script:WaitForChild('Info'))
gen.lastTurnDir = nil
gen.serverStorage = game:GetService('ServerStorage')
gen.generatedRooms = workspace.GeneratedRooms

function gen.getRandom(prevRoom)
    local totalChance = 0
    for _, info in ipairs(gen.info) do
        totalChance += info.Chance
    end
    local randomChance = gen.random:NextInteger(1, totalChance)
    local currentChance = 0
    local randomRoom = nil
    for i, info in ipairs(gen.info) do
        currentChance += info.Chance
        if randomChance <= currentChance then
            randomRoom = gen.serverStorage.Rooms[i]
            break
        end
    end

    local dir = gen.info[randomRoom.Name]['Direction']

    if (prevRoom.Name == randomRoom.Name)
        or (dir and dir == gen.lastTurnDir)
    then
        return gen.getRandom(prevRoom)
    else
        if dir then
            gen.lastTurnDir = dir
        end
        return randomRoom
    end

end

function gen.generate(prevRoom)
    local randomRoom = gen.getRandom(prevRoom)
    local newRoom = randomRoom:Clone()

    newRoom.PrimaryPart = newRoom.Entrance

    newRoom.Entrance.CanCollide = false
    newRoom.Entrance.Transparency = 1

    newRoom.Exit.CanCollide = false
    newRoom.Exit.Transparency = 1

    newRoom:PivotTo(prevRoom.Exit.CFrame)

    newRoom.Parent = gen.generatedRooms

    return newRoom
end

return gen

I'm very happy for solutions.

1 answer

Log in to vote
0
Answered by 1 year ago

Nevermind, i just put ipairs.

Basically, ipairs just broke the whole entire thing.

Sorry for wasting your time!

Ad

Answer this question