Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How can I make an NPC spawn with a random model each time I spawn it?

justvibin035

New Coder
Hi all. Code is below (GLUA). Trying to set up the code so that when an npc spawns in-game, it will spawn with a random model pulled from the innies table each time it is spawned. Right now it spawns with a random model once and each npc iteration after is the same model as the first. What can I tweak so that it will spawn with a random model on each iteration?



Code:
local innies = {
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_alfie.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_carl.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_donnie.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_fabrice.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_gilberto.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_islambek.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_john.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_jose.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_michael.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_ray.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_snippy.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_yasser.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/male/innie_yohannes.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/female/innie_dominique.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/female/innie_miia.mdl",
    "models/ishi/suno/halo_rebirth/npc/innie/female/innie_razer.mdl"
}


local Category = "URF (Hostile)"

local NPC = {
    Name = "Hostile - Rifleman",
    Class = "npc_combine_s",
    Model = table.Random(innies),
    Health = "150",
    Weapons = {"tfa_hr_swep_assault_rifle"},
    Category = Category
}


list.Set( "NPC", "npc_rifleman_hostile", NPC )
 
To make an NPC (Non-Playable Character) spawn with a random model each time you spawn it in Lua, you need to create an array or a table of possible models and then choose a random model from that array when spawning the NPC

Rich (BB code):
-- Define a table of possible NPC models
local npcModels = {
    "model1.mdl",
    "model2.mdl",
    "model3.mdl",
    -- Add more model paths as needed
}

-- Function to spawn an NPC with a random model
function SpawnRandomNPC()
    local randomModel = npcModels[math.random(1, #npcModels)] -- Choose a random model from the table
    local npc = ents.Create("npc_citizen") -- Replace "npc_citizen" with the appropriate NPC type for your game
    npc:SetModel(randomModel)
    npc:Spawn()
end

-- Example usage
SpawnRandomNPC()

In this example, we first define an array called npcModels containing the paths to the different NPC models you want to use. You can add as many model paths as you need to this array. In case you need to beautify lua code use this one Lua Beautifier Online to beautify Lua code

The SpawnRandomNPC function selects a random model from the npcModels array using math.random, and then it creates an NPC entity with that model and spawns it in the game.

You would need to adapt this code to your specific game engine or framework and make sure to replace "npc_citizen" with the correct entity type for the NPCs in your game. in case
 

New Threads

Buy us a coffee!

Back
Top Bottom