Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Custom NPC + PathService Problem?

I wanted to make a custom NPCs, and they'll spawn in random locations, while moving to different locations as well. I've used a gear script and editted it to make it work as a normal script. I've made it work with pathways, but I think I'd rather have it wander around to random places.

-- Randomized NPC Script Here
while wait(5) do
local paths = script.Parent.Pathways:GetChildren()

function getRandomValue(table)
    return table[math.random(#table)]
end

HATS = {
    48474294, -- Pinktastic Hair
    51353039, -- Bloxxer Cap
    18174901, -- Guardia in Exile
    30200035,
    48474313,
    19027209,
    16630147,
    19398258,
    16412796
}

HEADS = {
    6340213,
    6340101,
    6340227,
}

FACES = {
    "http://www.roblox.com/asset/?id=7074749", -- Chill
    "rbxasset://textures/face.png",
    "rbxasset://textures/face.png",
}

SHIRTS = {
    "http://www.roblox.com/asset/?id=8561740", -- Guest Shirt
    "http://www.roblox.com/asset/?id=3039068",
}

TEESHIRTS = {
        "http://www.roblox.com/asset/?id=1028594", -- Bloxxer
}

SKIN_COLORS = {
    BrickColor.new("Bright yellow"),
    BrickColor.new("Pastel brown"),
    BrickColor.new("Institutional white")
}


function getDummy()

    local dummy = game:GetService("InsertService"):LoadAsset(357106698):GetChildren()[1]

    local skinColor = getRandomValue(SKIN_COLORS)
    dummy.Head.BrickColor = BrickColor.new("Bright yellow")
    dummy["Right Arm"].BrickColor = BrickColor.new("Bright yellow")
    dummy["Left Arm"].BrickColor = BrickColor.new("Bright yellow")

    game:GetService("Debris"):AddItem(dummy, 120)
    dummy.NPC.Died:connect(function()
	wait(5)
        dummy:Destroy()
    end)

    dummy.Torso.BrickColor = BrickColor.random()
    dummy["Left Leg"].BrickColor = BrickColor.new('Black')
    dummy["Right Leg"].BrickColor = BrickColor.new('Black')

    local shirtTest = math.random()
    if shirtTest < .5 then
        dummy.Torso.Decal.Texture = getRandomValue(TEESHIRTS)
    elseif shirtTest < .7 then
        dummy.Shirt.ShirtTemplate = getRandomValue(SHIRTS)
    end     


    dummy.Head.face.Texture = getRandomValue(FACES)
    dummy.Name = "Person"

    return dummy
end


function getFans()
    local count = 0
    for _, child in pairs(Workspace:GetChildren()) do
        if child.Name == "Person" then
            count = count + 1
        end
    end
    return count
end
    for i = 1, 5 do
	number = math.random(1, (#paths))
        if getFans() <= 12 then
            local dummy = getDummy()
            dummy.NPC.WalkSpeed = math.random(11, 15)
            dummy.Target.Value = paths[math.random(1, (#paths))]
            dummy.Parent = Workspace
            dummy.Head:MakeJoints()
            wait()
            if math.random() > .2 then
                game:GetService("InsertService"):LoadAsset(getRandomValue(HATS)):GetChildren()[1].Parent = dummy
            end

            local torsoPosition = paths[number].Position

            local x = paths[number].Position.X
            local z = paths[number].Position.Z
            dummy:MoveTo(Vector3.new(math.random(x - 10, x + 10), paths[number].Position.Y, math.random(z - 10, z + 10)))
        end
    end
end

Here's the script in the dummy allowing it to walk, It may be helpful to solve the problem.

local function plusOrMinus(number)
	return math.random(number - 4, number + 4)	
end

local function randomize(v)
	return Vector3.new(plusOrMinus(v.X), plusOrMinus(v.Y), plusOrMinus(v.Z))
end

wait(math.random())
local walkspeed = script.Parent.NPC.WalkSpeed
local hum = script.Parent.NPC
local head = script.Parent.Head
local torso = script.Parent.Torso
local target = script.Parent.Target.Value
while true do
	if (torso.Position - target.Position).magnitude < 2 then
		hum.WalkSpeed = 0
	else
		hum.WalkSpeed = walkspeed
		hum:MoveTo(randomize(target.Position), target)
	end
	wait(1)
end

1 answer

✓ Accepted answer

The reason your NPCs don't move far is due to how their movement is randomized, this is just how balanced randomness works. It is just as likely for the NPC to move in a direction as it is to move in its opposite, making the NPC reverse direction many times and bringing it closer to where it started.

As for the error, it appears to happen on line 98 (not line 99 like it says). On that line it is trying to set the property to a number. I assume you want a randomized value of the paths table instead: dummy.Target.Value = paths[math.random(1, (#paths))]

1 comment

0

Would it work better to allow the NPCs to wander randomly instead of at certain places in a spawn?

This discussion is preserved as it was. New answers, comments, and votes are closed.