[Release] Spike Strips Code


#1

So a while back i was making a prop script but with the amount of work needed i dropped the project and worked on other things.

Here is one of those many scripts, Hasnt been tested in FX but was working flawlessly in CitMP.

May need some tweaking, But it does work, Both you and other players can see it and also it pops tires as it should.

Again dont ask me for help with this as i cant remember how to LUA its been over a year since i touched GTA.

Have fun with this i know its one of the most requested scripts or at least was.

If you like this, Leave a hearty comment and who knows i may release more hidden projects that TPRP had :wink:

This was the code implimented into my Police menu script.

local spikes_deployed = false
local obj1 = nil
local obj2 = nil
local obj3 = nil
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(10)
        if spikes_deployed then
            for peeps = 0, 64 do
                if NetworkIsPlayerActive(GetPlayerFromServerId(peeps)) then
                    
                    local currentVeh = GetVehiclePedIsIn(GetPlayerPed(GetPlayerFromServerId(peeps)), false)
                    if currentVeh ~= nil and currentVeh ~= false then
                        local currentVehcoords = GetEntityCoords(currentVeh, true)
                        local obj1coords = GetEntityCoords(obj1, true)
                        local obj2coords = GetEntityCoords(obj2, true)
                        local obj3coords = GetEntityCoords(obj3, true)
                        local DistanceBetweenObj1 = Vdist(obj1coords['x'], obj1coords['y'], obj1coords['z'], currentVehcoords['x'], currentVehcoords['y'], currentVehcoords['z'])
                        local DistanceBetweenObj2 = Vdist(obj2coords['x'], obj2coords['y'], obj2coords['z'], currentVehcoords['x'], currentVehcoords['y'], currentVehcoords['z'])
                        local DistanceBetweenObj3 = Vdist(obj3coords['x'], obj3coords['y'], obj3coords['z'], currentVehcoords['x'], currentVehcoords['y'], currentVehcoords['z'])
                        if DistanceBetweenObj1 < 2.2 or DistanceBetweenObj2 < 2.2 or DistanceBetweenObj3 < 2.2 then
                            
							TriggerServerEvent("police:spikes", currentVeh, peeps)
                            --TriggerEvent("police:Deploy")
                        end
                    end
                end
            end
        end
    end
end)

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        if spikes_deployed then
        	local obj1coords = GetEntityCoords(obj1, true)
			if GetDistanceBetweenCoords(GetEntityCoords(GetPlayerPed(-1)), obj1coords.x, obj1coords.y, obj1coords.z, true) > 100 then -- if the player is too far from his Spikes
           		SetEntityAsMissionEntity(obj1, false, false)
           		SetEntityAsMissionEntity(obj2, false, false)
           		SetEntityAsMissionEntity(obj3, false, false)
           		SetEntityVisible(obj1, false)
           		SetEntityVisible(obj2, false)
           		SetEntityVisible(obj3, false)
           		DeleteObject(obj1)
           		DeleteObject(obj2)
           		DeleteObject(obj3)
                DeleteEntity(obj1)
                DeleteEntity(obj2)
                DeleteEntity(obj3)
                obj1 = nil
                obj2 = nil
                obj3 = nil
                exports.pNotify:SendNotification({text = "Removing spikes! (D>100)",type = "error",queue = "left",timeout = 3000,layout = "centerRight"})
                spikes_deployed = false
        	end
        end
	end
end)
RegisterNetEvent("police:dietyres")
AddEventHandler("police:dietyres", function(currentVeh)
    SetVehicleTyreBurst(currentVeh, 0, true, 0)
    SetVehicleTyreBurst(currentVeh, 1, true, 1)
    SetVehicleTyreBurst(currentVeh, 2, true, 1)
    SetVehicleTyreBurst(currentVeh, 3, true, 1)
    SetVehicleTyreBurst(currentVeh, 4, true, 3)
    SetVehicleTyreBurst(currentVeh, 5, true, 4)
end)

RegisterNetEvent("police:dietyres2")
AddEventHandler("police:dietyres2", function(peeps)
    SetVehicleTyreBurst(GetVehiclePedIsIn(GetPlayerPed(GetPlayerFromServerId(peeps)), false), 0, true, 0)
    SetVehicleTyreBurst(GetVehiclePedIsIn(GetPlayerPed(GetPlayerFromServerId(peeps)), false), 1, true, 1)
    SetVehicleTyreBurst(GetVehiclePedIsIn(GetPlayerPed(GetPlayerFromServerId(peeps)), false), 2, true, 1)
    SetVehicleTyreBurst(GetVehiclePedIsIn(GetPlayerPed(GetPlayerFromServerId(peeps)), false), 3, true, 1)
    SetVehicleTyreBurst(GetVehiclePedIsIn(GetPlayerPed(GetPlayerFromServerId(peeps)), false), 4, true, 3)
    SetVehicleTyreBurst(GetVehiclePedIsIn(GetPlayerPed(GetPlayerFromServerId(peeps)), false), 5, true, 4)
end)

function spikes()
	TriggerEvent("police:Deploy")
end
RegisterNetEvent("police:Deploy")
AddEventHandler("police:Deploy", function()
    Citizen.CreateThread(function()
        if not spikes_deployed then
            local spikes = GetHashKey("p_stinger_04")
            RequestModel(spikes)
            while not HasModelLoaded(spikes) do
                Citizen.Wait(0)
            end
            exports.pNotify:SendNotification({text = "Deploying spikes!",type = "error",queue = "left",timeout = 3000,layout = "centerRight"}) 
            local playerheading = GetEntityHeading(GetPlayerPed(-1))
            coords1 = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 3, 10, -0.7)
            coords2 = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 0, -5, -0.5)
            obj1 = CreateObject(spikes, coords1['x'], coords1['y'], coords1['z'], true, true, true)
            obj2 = CreateObject(spikes, coords2['x'], coords2['y'], coords2['z'], true, true, true)
            obj3 = CreateObject(spikes, coords2['x'], coords2['y'], coords2['z'], true, true, true)
			SetEntityHeading(obj1, playerheading)
            SetEntityHeading(obj2, playerheading)
            SetEntityHeading(obj3, playerheading)
            AttachEntityToEntity(obj1, GetPlayerPed(-1), 1, 0.0, 4.0, 0.0, 0.0, -90.0, 0.0, true, true, false, false, 2, true)
            AttachEntityToEntity(obj2, GetPlayerPed(-1), 1, 0.0, 8.0, 0.0, 0.0, -90.0, 0.0, true, true, false, false, 2, true)
            AttachEntityToEntity(obj3, GetPlayerPed(-1), 1, 0.0, 12.0, 0.0, 0.0, -90.0, 0.0, true, true, false, false, 2, true)
            Citizen.Wait(10)
            DetachEntity(obj1, true, true)
            DetachEntity(obj2, true, true)
            DetachEntity(obj3, true, true)
            spikes_deployed = true
        else
        	spikes_deployed = false
            exports.pNotify:SendNotification({text = "Removing spikes!",type = "error",queue = "left",timeout = 3000,layout = "centerRight"}) 
            SetEntityCoords(obj1, -5000.0, -5000.0, 20.0, true, false, false, true)
            SetEntityCoords(obj2, -5000.0, -5000.0, 20.0, true, false, false, true)
            SetEntityCoords(obj3, -5000.0, -5000.0, 20.0, true, false, false, true)
            Citizen.InvokeNative(0xB736A491E64A32CF, Citizen.PointerValueIntInitialized(obj1))
            Citizen.InvokeNative(0xB736A491E64A32CF, Citizen.PointerValueIntInitialized(obj2))
            Citizen.InvokeNative(0xB736A491E64A32CF, Citizen.PointerValueIntInitialized(obj3))
            FIX_DeleteObject(obj1)
            FIX_DeleteObject(obj2)
            FIX_DeleteObject(obj3)
            obj1 = nil
            obj2 = nil
            obj3 = nil
        end
	end)
end)

Dont be like ‘OMG IT DOESNT WORK’ Thats because you have to implement it into your server either add it to a menu or a keybind etc.


#2

You’re using client side natives on your server side script. I can’t almost certainly say that this will not work.


#3

woops back to front my bad…


#4

Even after your edit, the server.lua is client side script… :confused:


#5

Updated the code :slight_smile: 20char…


#6

hmm cool good job dude



#7

I managed to get it working into my menu, any idea as to why its not popping tires? The only code i changed was the notifications, i made it send a chat message rather than pNotify. Is it because i am running them over with my own car?