Bully-Board

Bully Modding Section => Modding Questions/Help => Modding Questions/Help Archives => Topic started by: UltimateGamer9 on May 16, 2016, 08:12:48 AM

Title: how to make peds spawn randomly in lua?
Post by: UltimateGamer9 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
Title: Re: how to make peds spawn randomly in lua?
Post by: DaBOSS54320 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