#ServerScriptService.PlantSpawner:55: attempt to index nil with 'FindFirstChildWhichIsA'

1 messages · Page 1 of 1 (latest)

grand notch
#

Error: ServerScriptService.PlantSpawner:55: attempt to index nil with 'FindFirstChildWhichIsA'

Code:

while task.wait(3) do
    local randrar = getRandomRarity()
    local PlantModels = game.ReplicatedStorage.Plants[randrar[1]]
    local randnum = math.random(1, #PlantModels:GetChildren())
    
    local CNPC: Model = Plants.ClonePlant(PlantModels:GetChildren()[randnum].Name, workspace.Plants)
    local CHum = CNPC:FindFirstChildWhichIsA("Humanoid")
    local CTorso = CNPC:FindFirstChild("Torso")
    
    if not CTorso then
        CNPC:Destroy()
    end
    
    
    CNPC:PivotTo(workspace.Enter.CFrame)
    
    CHum:MoveTo(workspace.Exit.Position)
    moveTo(CHum, workspace.Exit.Position, function()
        CNPC:Destroy()
    end)
end
gilded loom
#

you should use :FindFirstChildOfClass("Humanoid") instead of :FindFirstChildWhichIsA("Humanoid")

#

and instead of checking if the character has a torso, checking if the character has a HumanoidRootPart should be better than

#

Replace your code from this:

local CTorso = CNPC:FindFirstChild("Torso")
    
    if not CTorso then
        CNPC:Destroy()
    end

To this:

local RootPart = CNPC:FindFirstChild("HumanoidRootPart")

   if not RootPart then
      return
   end
grand notch
#

im change this code but humanoid already dont working

ServerScriptService.PlantSpawner:55: attempt to index nil with 'FindFirstChildOfClass'

gilded loom
#

so this line:

  local randnum = math.random(1, #PlantModels:GetChildren())

it gives you a random index from your plantmodels?

grand notch
#

yes

gilded loom
#

if so delete the :GetChildren() on this line:

local CNPC: Model = Plants.ClonePlant(PlantModels:GetChildren()[randnum].Name, workspace.Plants)
grand notch
#

1 is not a valid member of Folder "ReplicatedStorage.Plants.Rare"

#

i have this

gilded loom
#

but is that on plantmodels have anythings named any number?

#

or you can do local CNPC: Model = Plants.ClonePlant(PlantModels[tostring(randnum)].Name, workspace.Plants)

#

but what is Plants.ClonePlant are?

#

is it a function?

lyric flax
#

I'm assuming you mean to use (), but just checking to make sure

#

Scratch that, I didn't notice the latter half of that, it's clear you used the right thing.

#

Could you provide the code for the randrar function?

#

Well, actually, I guess if that was the problem it wouldn't be able to get the children of the plant model

#

Only thing I can think of is you might accidentally be sending workspace.Plants as a second parameter instead of game.ReplicatedStorage.Plants

grand notch
#
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:FindFirstChildWhichIsA("Humanoid")

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