Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Trying to add a part to randomly generated NPC?

Asked by 3 days ago

So I have been trying to add glasses, hair and the like to a randomly generated NPC in my game, but I have not been able to do it successfully.

My code goes like this:

room = script.Parent.Parent.Parent.Parent

enums = {"The names I wrote"}
enums2 = {BrickColor.new("Pastel brown"),BrickColor.new("Brick yellow"),BrickColor.new("Pine Cone"),BrickColor.new("Dirt brown"),BrickColor.new("Wheat")}
script.Parent.ClickDetector.MouseClick:connect(function()
if not room.Human:FindFirstChildOfClass("Model") then
    local color = enums2[math.random(1,#enums2)]
    script.Parent.Sound:Play()
        local human = game.Lighting.Human:Clone()
    for i,v in pairs(human:GetChildren()) do
        if v:IsA("Part") then
            v.BrickColor = color
        end
    end

    human.Name = enums[math.random(1,#enums)]
    human.Parent = room.Human
    human:MakeJoints()

human.HumanoidRootPart.CFrame = room.Hspawn.CFrame
wait(0.5)
            room.Ground.hit:Play()

    end
end)

This code was salvaged from a friends game, but we fell out of contact.

Im very new to scripting, and have tried to create a new part with a special mesh in it, with the mesh and texture ID of a pair of glasses from the catalog.

I tried this both in the above script and in its own script:

local human = script.Parent.Parent.Parent.Parent.Human:FindFirstChildOfClass("Model")

script.Parent.ClickDetector.MouseClick:connect(function()
    wait(1)

    if human.Name("Name") then
        local hat = Instance.new("Part",human.Head)
        hat.Name = "accessory"

        local mesh = Instance.new("SpecialMesh")
        mesh.Name = "glasses"
        mesh.Scale = Vector3.new(1,1,1)
        mesh.MeshID = "rbxassetid://4850101059"
        mesh.TextureID = "rbxassetid://4850106355"
        mesh.Parent = human.Head.hat

        hat:SetPrimaryPartCFrame(human.Head)
        local Weld = Instance.new("WeldConstraint",human.Head)
        Weld.Part0 = human.Head
        Weld.Part1 = human.Head.hat
    end
    if not human.Name("Name") then
        return nil
    end
end)

Im at a loss and as Im very inexperienced, Im sure theres a huge glaring issue somewhere in these two scripts, and Id like a bit of help in figuring this out. I like to think i wrote it "fine" but I dont know why it wont work.

Answer this question