Jump to content

Random object spawn


Anubhav

Recommended Posts

The method din't work.

It has bugs, it's just making objects too high in the air

  
items = { 
    "Water Bottle", 
    "Tea", 
    "Fence", 
    'Morphine', 
    "Pizza", 
    "Chicken Burger", 
    "Pepsi", 
    "Fanta", 
    "Engine", 
    "Wheel" 
} 
function getXY( ) 
    return math.random(9999), math.random(9999), math.random(9999) 
end 
  
function spawnObjects( ) 
    local x, y, z = getXY( ) 
    z = getGroundPosition( x, y, z ) 
--  local x, y, z =  
    local object = createObject( 1271, x, y, z + 0.3 ) 
    setElementData( object, "item", items[ math.random( #items ) ] ) 
    createBlip( 1, x, y, z ) 
    setTimer( spawnObjects, 3000, 1 ) 
end 
spawnObjects( ) 
  

Link to comment

Because minimal x and y is -1500 and maximum 1500. Try this

items = { 
    "Water Bottle", 
    "Tea", 
    "Fence", 
    'Morphine', 
    "Pizza", 
    "Chicken Burger", 
    "Pepsi", 
    "Fanta", 
    "Engine", 
    "Wheel" 
} 
function getXY( ) 
    return math.random(-1500, 1500), math.random(-1500, 1500), 0 
end 
  
function spawnObjects( ) 
    local x, y, z = getXY( ) 
    z = getGroundPosition( x, y, z ) 
    local object = createObject( 1271, x, y, z + 0.3 ) 
    setElementData( object, "item", items[ math.random( #items ) ] ) 
    createBlip( 1, x, y, z ) 
    setTimer( spawnObjects, 3000, 1 ) 
end 
spawnObjects( ) 

Link to comment
Because minimal x and y is -1500 and maximum 1500. Try this
items = { 
    "Water Bottle", 
    "Tea", 
    "Fence", 
    'Morphine', 
    "Pizza", 
    "Chicken Burger", 
    "Pepsi", 
    "Fanta", 
    "Engine", 
    "Wheel" 
} 
function getXY( ) 
    return math.random(-1500, 1500), math.random(-1500, 1500), 0 
end 
  
function spawnObjects( ) 
    local x, y, z = getXY( ) 
    z = getGroundPosition( x, y, z ) 
    local object = createObject( 1271, x, y, z + 0.3 ) 
    setElementData( object, "item", items[ math.random( #items ) ] ) 
    createBlip( 1, x, y, z ) 
    setTimer( spawnObjects, 3000, 1 ) 
end 
spawnObjects( ) 

it's -3000,3000 not -1500,1500

Link to comment
local Timers = {} 
items = { 
    "Water Bottle", 
    "Tea", 
    "Fence", 
    'Morphine', 
    "Pizza", 
    "Chicken Burger", 
    "Pepsi", 
    "Fanta", 
    "Engine", 
    "Wheel" 
} 
function getXY( ) 
    return math.random(-1500, 1500), math.random(-1500, 1500) 
end 
  
function spawnObjects( ) 
    local x, y = getXY( ) 
    local object = createObject( 1271, x, y, 0 ) 
    setElementData( object, "item", items[ math.random( #items ) ] ) 
    setElementOnGround(object) 
    createBlip( 1, x, y, 100 ) 
    setTimer( spawnObjects, 3000, 1 ) 
end 
spawnObjects( ) 
  
function setElementOnGround(element) 
    local eX,eY,eZ = getElementPosition(element) 
    Timers[element] = setTimer( 
    function(x,y,z) 
    local succeed, _, _, groundZ = processLineOfSight(x, y, 3000, x, y, -3000) 
        if succeed then 
            local wZ = getWaterLevel(x, y, 100) 
            z = (wZ and math.max(groundZ, wZ) or groundZ) + getElementDistanceFromCentreOfMassToBaseOfModel(element) 
            setElementPosition(x,y,z) 
            killTimer(Timers[element]) 
            Timers[element] = nil 
        end 
    end,100,0,eX,eY,eZ) 
end 

setElementOnGround function codes&idea rights back to who created the freeroam, i have just make it as a function

Edit:

I haven't test, so im not sure if it's gonna works

Edited by Guest
Link to comment

ALw7sh @ Thanks for helping me.

There were bugs in setElementPosition argument 1 element, and element wasn't defined in timer. I fixed it and it worked like charm!

FIXED VERSION IF SOMEONE NEEDS:

  
local Timers = {} 
items = { 
    "Water Bottle", 
    "Tea", 
    "Fence", 
    'Morphine', 
    "Pizza", 
    "Chicken Burger", 
    "Pepsi", 
    "Fanta", 
    "Engine", 
    "Wheel" 
} 
function getXY( ) 
    return math.random(-1500, 1500), math.random(-1500, 1500) 
end 
  
function setElementOnGround(element) 
    local eX,eY,eZ = getElementPosition(element) 
    Timers[element] = setTimer( 
    function(x,y,z, element) 
    local succeed, _, _, groundZ = processLineOfSight(x, y, 3000, x, y, -3000) 
        if succeed then 
            local wZ = getWaterLevel(x, y, 100) 
            z = (wZ and math.max(groundZ, wZ) or groundZ) + getElementDistanceFromCentreOfMassToBaseOfModel(element) 
            setElementPosition(element, x,y,z) 
            killTimer(Timers[element]) 
            Timers[element] = nil 
        end 
    end,100,0,eX,eY,eZ, element) 
end 
  
function spawnObjects( ) 
    local x, y = getXY( ) 
    local object = createObject( 1271, x, y, 0 ) 
    setTimer( function() 
    setElementData( object, "item", items[ math.random( #items ) ] ) 
    setElementOnGround(object) 
    createBlip( x, y, 1, 0 ) 
    end, 200, 1 ) 
    setTimer( spawnObjects, 3000, 1 ) 
end 
spawnObjects( ) 
  
  

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...