News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: how to make peds spawn randomly in lua?  (Read 3025 times)

0 Members and 3 Guests are viewing this topic.

Offline UltimateGamer9

  • Full Member
  • ***
  • Posts: 190
  • Gender: Male
    • View Profile
how to make peds spawn randomly in lua?
« on: May 16, 2016, 08:12:48 AM »
so how do i make peds randomly spawn in lua? example gary, he sometimes will spawn here,sometimes he will spawn there

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: how to make peds spawn randomly in lua?
« Reply #1 on: May 16, 2016, 10:05:00 AM »
Code: [Select]
-- Step 1: Setup a table of positions:
local positions = {
{270,-110,6}, -- this is a table with 3 numbers in it, X, Y, and Z coordinates.
-- You can add more tables here, I don't know a lot of coordinates by memory.
}

-- Step 2: Get a random position:
local tableSize = table.getn(positions) -- table.getn basically gets the size of a table, and we store that value in a variable
local i = math.random(1,tableSize) -- math.random gets a random value between 2 numbers (inclusive), we use 1 and the size of the table here
local position = positions[i] -- get one of the position tables from the table of positions using the random index

-- Step 3: Use the randomly selected table as a ped position:
local ped = PedCreateXYZ(4,position[1],position[2],position[3]) -- position[1] is the first value in the table we got, the X coord, position[2] is the Y, and position[3] the Z