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

attempt to index nil with 'HumanoidRootPart' How to fix?

Asked by 3 years ago

Im trying to script a egg hatching system and im getting this error on line 55 of this script, how can i fix it?

local clickDetector = script.Parent

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataModule = require(ReplicatedStorage:WaitForChild("DataModule"))
local Pets = ReplicatedStorage:WaitForChild("Pets")

local function ChoosePet(player)
    local PlayerGui = player:WaitForChild("PlayerGui")
    local PetGui = PlayerGui.PetGui
    local EggIcon = PetGui.EggIcon
    local PetImages = PetGui.PetImages

    local timer = tick()

    EggIcon.Visible = true

    while tick() - timer < DataModule.EggOpeningLength do
        wait(.1)
        EggIcon.Rotation = math.random(-9,9)
    end

    local petNumber = math.random(1,100)
    local petFound = false
    local pet = nil
    while petFound == false do
        for i, v in pairs(DataModule.Pets) do
            if math.random(1,v) == petNumber then
                petFound = true
                pet = i
            end
        end
    end

    EggIcon.Visible = false

    local PetImage = PetImages:FindFirstChild(pet)
    PetImage.Visible = true
    wait(2)
    PetImage.Visible = false

    return pet
end


clickDetector.MouseClick:Connect(function(player)

    if player.leaderstats.Money.Value >= DataModule.EggCost then
        player.leaderstats.Money.Value = player.leaderstats.Money.Value - DataModule.EggCost

        local petOpened = ChoosePet(player)

        print(petOpened)

        local pet = Pets:FindFirstChild(petOpened):clone()
        pet.Position = player.Character.HumanoidRootPart.Position
        pet.Parent = game.Workspace
    end
end)
0
Try using player.Character.Humanoid.Torso.CFrame DuckyRobIox 280 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Your error says Character is nil, doesn't exists. So you must do a variable for the player's character, i recommend doing for example right after clickDetector.MouseClick:Connect(function(player) do:

local char = player.Character

And then do this in the line 55:

pet.Position = char.HumanoidRootPart.Position

Hope this works! If it doesn't, at least you know what's the error :D

0
ohhhhh okay thanks coolflamingoiscool 9 — 3y
Ad

Answer this question