Jump to content

Give placed objects chance to not spawn on the map


Recommended Posts

Hello. I want to block some roads with barriers and make it so that every added barrier has a chance to not spawn on the map start (so, for example, Doherty could be absolutely blocked with the barriers at first map load, and after reloading it only have a single barrier be present). How do I code that properly? Thanks in advance. 

Link to comment

Yes but not in the way u think.

U can do something like this ->

local object 
local objects = {}

CreateRandomObjects = function(player,commandName)
  		local number = math.random(1,2)
  	if number == 2 then 
  	local object = createObject(objects[number][1],objects[number][2],objects[number][3],objects[number][4],objects[number][5],objects[number][6],robjects[number][7])
    -- we just get from objects table based on number the object and his model and x,y,z,rotation x ,rotation y, rotation z, and u can add more to it.
  elseif number == 1 then 
  	local object = createObject(objects[number][1],objects[number][2],objects[number][3],objects[number][4],objects[number][5],objects[number][6],robjects[number][7])
  	
  	end
 end 
  addCommandHandler ( "randomobject", CreateRandomObjects,false,false)

well we can add more objects at the same time not only one but i added only one for example.

I made a command for this u can do this thing on marker hit or any event u want.

Edited by FlorinSzasz
Link to comment
  1. First, you need to create a table that stores the information about each road and its barriers. Each road will have an associated number of barriers and a flag indicating whether it should be blocked initially or not.

  2. During the map load or initialization, you'll check the flags and add barriers to the roads accordingly.

  3. After each map reload, you'll remove a barrier randomly from the initially blocked roads.

Here's a basic implementation of the concept:

 

-- Define the roads and barriers data table
local roadsData = {
    { name = "Doherty", numBarriers = 3, blockedInitially = true },
    { name = "ExampleRoad1", numBarriers = 2, blockedInitially = false },
    { name = "ExampleRoad2", numBarriers = 1, blockedInitially = false },
    -- Add more roads here as needed
}

-- Function to add barriers to a road
function addBarriersToRoad(roadData)
    local roadName = roadData.name
    local numBarriers = roadData.numBarriers

    for i = 1, numBarriers do
        -- Add code to create barriers on the road (e.g., createObject)
        -- The exact code depends on your map and barrier objects
        -- Example: createObject(x, y, z, model)
    end
end

-- Function to remove a random barrier from a road
function removeRandomBarrierFromRoad(roadData)
    local roadName = roadData.name
    local numBarriers = roadData.numBarriers

    if numBarriers > 0 then
        local barrierIndex = math.random(1, numBarriers)

        -- Remove code to destroy the barrier (e.g., destroyElement)
        -- The exact code depends on how you created the barriers
        -- Example: destroyElement(barrierObjects[roadName][barrierIndex])
        -- Make sure to keep track of the barrier objects using a table if needed.
    end
end

-- Function to initialize roads and barriers
function initializeRoadsAndBarriers()
    for _, roadData in ipairs(roadsData) do
        if roadData.blockedInitially then
            addBarriersToRoad(roadData)
        end
    end
end

-- Initialize the roads and barriers when the resource starts
addEventHandler("onResourceStart", resourceRoot, initializeRoadsAndBarriers)

-- Function to handle map reload event
function onMapReload()
    for _, roadData in ipairs(roadsData) do
        if roadData.blockedInitially then
            removeRandomBarrierFromRoad(roadData)
        end
    end
end

-- Register the map reload event
addEventHandler("onMapLoad", root, onMapReload)

In this example, we defined the roads and barriers in the roadsData table. The initializeRoadsAndBarriers function checks the blockedInitially flag for each road and adds barriers accordingly during the resource start. The onMapReload function removes one random barrier from the initially blocked roads after each map reload.

Remember that you may need to adapt the code depending on your specific map and barrier objects. Additionally, you should handle errors and edge cases to ensure the script works correctly in various scenarios.

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...