#attempt to index nil with 'WaitForChild'

1 messages · Page 1 of 1 (latest)

weak sapphire
#

error on 55, and attempt to index nil with ':PivotTo' on 57 line

local Plants = require(game.ReplicatedStorage:WaitForChild("Plants"))
local SharedBases = require(game.ServerStorage:WaitForChild("SharedBases"))
local Path = workspace:WaitForChild("Doroga")

local HumMoveTasks = {}

local raritydata = {
    {"Common"; 2; Color3.fromRGB(118, 213, 89)},
    {"Rare"; 5; Color3.fromRGB(88, 213, 205)},
    {"Mythic"; 16; Color3.fromRGB(213, 71, 71)}
}

local function getRandomRarity()
    for i, v in ipairs(raritydata) do
        local RolledNumber = math.random(1, v[2])
        if RolledNumber == 1 and v[2] > raritydata[1][2] then
            return {
                v[1];
                v[2];
                v[3];
            }
        end
    end
    
    return raritydata[1]
end

local function moveTo(Humanoid: Humanoid, Target: Vector3, AndThen)
    local id = tick()
    HumMoveTasks[Humanoid] = id
    
    task.spawn(function()
        Humanoid:MoveTo(Target)
        Humanoid.MoveToFinished:Wait()
        
        if HumMoveTasks[Humanoid] ~= id then return end
        
        local dist = (Humanoid.RootPart.Position - Target).Magnitude
        if dist < 2 then
            if AndThen then    
                AndThen()
            end
        else
            moveTo(Humanoid, Target, AndThen)
        end
    end)
end

while task.wait(3) do
    local randrar = getRandomRarity()
    local PlantModels = game:GetService("ReplicatedStorage").Plants[randrar[1]]
    local randnum = math.random(1, #PlantModels:GetChildren())
    
    local CNPC: Model = Plants.ClonePlant(PlantModels:GetChildren()[randnum].Name, workspace)
    local CHum: Humanoid = CNPC:WaitForChild("Humanoid")

    CNPC:PivotTo(workspace.Enter.CFrame)
    
    CHum:MoveTo(workspace.Exit.Position)
    moveTo(CHum, workspace.Exit.Position, function()
        CNPC:Destroy()
    end)
end
#

this is server script, and module script with rarity folders

local Module = {}

local Plants = {}

local raritydata = {
    {"Common"; 2; Color3.fromRGB(118, 213, 89)},
    {"Rare"; 5; Color3.fromRGB(88, 213, 205)},
    {"Legendary"; 10; Color3.fromRGB(255, 212, 57)},
    {"Mythic"; 16; Color3.fromRGB(213, 71, 71)}
}

local PlantRarity = {}

for _, rarity in script:GetChildren() do
    if rarity:IsA("Folder") then
        for _, plant in rarity:GetChildren() do
            table.insert(Plants, plant)
            Plants[plant.Name] = plant
            PlantRarity[plant.Name] = rarity.Name
        end
    end
end

local function GetRarityColor(rarityName)
    for _, data in ipairs(raritydata) do
        if data[1] == rarityName then
            return data[3]
        end
    end
    
    return Color3.new(1, 1, 1)
end

function Module.ClonePlant(Name: string, Parent: Instance)
    local CNPC = Plants[Name]:Clone()
    CNPC.Parent = Parent
    
    local rarity = PlantRarity[Name]
    local Color = GetRarityColor(rarity)
    
    local CHeadInfo = script.HeadInfo:Clone()
    CHeadInfo.Parent = CNPC.Torso
    CHeadInfo.Username.Text = CNPC.Name
    CHeadInfo.Price.Text = CNPC:GetAttribute("Price").. "$"
    CHeadInfo.Rarity.Text = rarity
    CHeadInfo.Rarity.TextColor3 = Color
    CHeadInfo.Production.Text = CNPC:GetAttribute("Production").. "$/s"
end

    
return Module



charred cargo
#

CNPC:PivotTo(workspace.Enter.CFrame) okay I found the issue

#

You haven't reference the part.

#

Always reference parts instead of doing workspace.