fivem scripting here is my server lua file for a script I want it to delete a item from…
- User
fivem scripting here is my server lua file for a script I want it to delete a item from the table if its been in for longer then 180 seconds
-- server.lua local oil_slicks = {}
-- Server event to create oil slick RegisterNetEvent('oil:create_oil_slick') AddEventHandler('oil:create_oil_slick', function(coords) -- Add oil slick to the table table.insert(oil_slicks, coords) -- Trigger client event to create slick TriggerClientEvent('oil:create_petrol_decal', -1, coords, oil_slicks) end)
-- Server event to delete oil slick RegisterNetEvent('oil:delete_oil_slick') AddEventHandler('oil:delete_oil_slick', function(coords) -- Find the index of the oil slick to delete local index_to_delete = nil for k, v in pairs(oil_slicks) do if v == coords then index_to_delete = k break end end
-- Remove oil slick from the table if index_to_delete then table.remove(oil_slicks, index_to_delete) end -- Trigger client event to delete slick TriggerClientEvent('oil:delete_petrol_decal', -1, coords)end)
- Assistant
You can achieve this by creating a new function that will run every second and check the age of the oil_slicks.
Here's the updated server.lua file with the new function:
-- server.lua local oil_slicks = {} -- Server event to create oil slick RegisterNetEvent('oil:create_oil_slick') AddEventHandler('oil:create_oil_slick', function(coords) -- Add oil slick with timestamp to the table table.insert(oil_slicks, {coords = coords, time = os.time()}) -- Trigger client event to create slick TriggerClientEvent('oil:create_petrol_decal', -1, coords, oil_slicks) end) -- Server event to delete oil slick RegisterNetEvent('oil:delete_oil_slick') AddEventHandler('oil:delete_oil_slick', function(coords) -- Find the index of the oil slick to delete local index_to_delete = nil for k, v in pairs(oil_slicks) do if v.coords == coords then index_to_delete = k break end end -- Remove oil slick from the table if index_to_delete then table.remove(oil_slicks, index_to_delete) end -- Trigger client event to delete slick TriggerClientEvent('oil:delete_petrol_decal', -1, coords) end) -- Function to remove old oil slicks from the table function remove_old_oil_slicks() local currentTime = os.time() local timeLimit = 180 -- seconds for k, v in pairs(oil_slicks) do if currentTime - v.time >= timeLimit then -- Trigger client event to delete old slick TriggerClientEvent('oil:delete_petrol_decal', -1, v.coords) -- Remove oil slick from the table table.remove(oil_slicks, k) end end end -- Set the function to run every second Citizen.CreateThread(function() while true do remove_old_oil_slicks() Citizen.Wait(1000) end end)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.