I'm working on a fivem oil slick script in lua here is the client -- This script allows…
- User
I'm working on a fivem oil slick script in lua
here is the client
-- This script allows the user to throw an item to create an oil slick this will make cars that hit it slide local AnimDict = "core"; local AnimName = "ent_anim_paparazzi_flash" local WeaponModel = "w_ex_oiltub" local Animations = {"anim@heists@ornate_bank@thermal_charge", "cover_eyes_intro"} local OilSlickEquiped = false local ped = GetPlayerPed(-1) local slipping = false
--table for oil slicks local_oil_slicks = {}
--local table of coords to handle local coords_to_handle = {}
--function for adding slip to vehicle function AddSlip() --create a thread Citizen.CreateThread(function() --get vehicle ped is in local vehicle = GetVehiclePedIsIn(ped, false) local old_val = GetVehicleHandlingFloat(vehicle, "CHandlingData", "fTractionCurveLateral") --set new traction value print('car should be slipping') SetVehicleHandlingFloat(vehicle, "CHandlingData", "fTractionCurveLateral", old_val * 6) --wait 7 seconds Wait(7000) --set traction back to old value print('car should no longer be slipping') SetVehicleHandlingFloat(vehicle, "CHandlingData", "fTractionCurveLateral", old_val) --set slipping to false slipping = false end) end
--event to check local table and remove slick at location RegisterNetEvent('oil:delete_petrol_decal') AddEventHandler('oil:delete_petrol_decal', function(coords) for k,v in pairs(coords_to_handle) do if v == coords then RemoveDecal(k) --check local oil_slicks table and remove slick at location for k,v in pairs(local_oil_slicks) do if v == coords then local_oil_slicks[k]=nil end end end end end)
Citizen.CreateThread(function() while true do --if player in vehicle if IsPedInAnyVehicle(ped,false) and not slipping then for k,v in pairs(local_oil_slicks) do --get distance to slick local dist = #(GetEntityCoords(ped) - v) if dist < 4 then --tell server to tell clients to delete TriggerServerEvent('oil:delete_oil_slick', v) if slipping == false then slipping = true AddSlip() end end end end Wait(200) end end)
--function to create oil slick function CreateOilSlick(handle) -- thread do check for collision with ground Citizen.CreateThread(function() while true do --get coords of entity local coords = GetEntityCoords(handle) --draw line to it -- DrawLine(coords.x, coords.y, coords.z, coords.x, coords.y, coords.z - 10.0, 255, 0, 0, 255) local h = GetEntityHeightAboveGround(handle) --if collided with ground if h <= 0.3 then local coords = GetEntityCoords(handle) TriggerServerEvent('oil:create_oil_slick', coords) break end Wait(0) end end) end
Citizen.CreateThread(function() while true do ped = GetPlayerPed(-1) if OilSlickEquiped == false then if GetSelectedPedWeapon(ped) == 277905663 then OilSlickEquiped = true; end else if IsPedShooting(ped) then OilSlickEquiped = false; local pos = GetEntityCoords(ped) Wait(100) local handle = GetClosestObjectOfType(pos.x,pos.y,pos.z,50.0,GetHashKey(WeaponModel),false,false,false) if handle ~= 0 then CreateOilSlick(handle) end end end Wait(0) end end)
- Assistant
To add a timer to delete the oil slick after 1 minute on the server, you can make use of the Citizen.CreateThread function. Here's how you can modify your existing server.lua script to add a timer:
-- 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) -- Add a timer to delete the oil slick after 1 minute (60000 milliseconds) Citizen.CreateThread(function() Citizen.Wait(60000) TriggerEvent('oil:delete_oil_slick', coords) end) 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) - User
I want it to check first if the oil still exists
- Assistant
To check if the oil slick still exists before deleting it, you can add a check inside the function that is called after the timer expires. Here's the updated server.lua script:
-- 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) -- Add a timer to delete the oil slick after 1 minute (60000 milliseconds) Citizen.CreateThread(function() Citizen.Wait(60000) -- Check if the oil slick still exists local index_to_delete = nil for k, v in pairs(oil_slicks) do if v == coords then index_to_delete = k break end end -- If the oil slick exists, trigger the event to delete it if index_to_delete then TriggerEvent('oil:delete_oil_slick', coords) end end) 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)