MilesFox92 Posted July 31, 2023 Share Posted July 31, 2023 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
#!_A7M8D Posted August 2, 2023 Share Posted August 2, 2023 Hello You only need createObject destroyElement Cheers! Link to comment
MilesFox92 Posted August 3, 2023 Author Share Posted August 3, 2023 On 02/08/2023 at 14:01, #!_A7M8D said: Hello You only need createObject destroyElement Cheers! Is there any "Random" function to make these either happen or not happen? 1 Link to comment
FlorinSzasz Posted August 3, 2023 Share Posted August 3, 2023 (edited) 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 August 3, 2023 by FlorinSzasz Link to comment
ExMohmD Posted August 4, 2023 Share Posted August 4, 2023 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. During the map load or initialization, you'll check the flags and add barriers to the roads accordingly. 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
MilesFox92 Posted August 4, 2023 Author Share Posted August 4, 2023 Big thanks to both of You! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now