diff --git a/client.lua b/client.lua index 01c6329..c3bdd8d 100644 --- a/client.lua +++ b/client.lua @@ -130,6 +130,7 @@ end) --Spike Strip Spawn Event local SpawnedSpikes = {} + RegisterNetEvent('SEM_InteractionMenu:Spikes-SpawnSpikes') AddEventHandler('SEM_InteractionMenu:Spikes-SpawnSpikes', function(Length) if IsPedInAnyVehicle(PlayerPedId(), false) then @@ -137,29 +138,70 @@ AddEventHandler('SEM_InteractionMenu:Spikes-SpawnSpikes', function(Length) return end - local SpawnCoords = GetOffsetFromEntityInWorldCoords(GetPlayerPed(PlayerId()) , 0.0, 2.0, 0.0) + local ped = PlayerPedId() + local SpawnCoords = GetOffsetFromEntityInWorldCoords(ped, 0.0, 2.0, 0.0) + + local group = {} + for a = 1, Length do - local Spike = CreateObject(GetHashKey('P_ld_stinger_s'), SpawnCoords.x, SpawnCoords.y, SpawnCoords.z, 1, 1, 1) + local Spike = CreateObject(GetHashKey('P_ld_stinger_s'), SpawnCoords.x, SpawnCoords.y, SpawnCoords.z, true, true, true) local NetID = NetworkGetNetworkIdFromEntity(Spike) + SetNetworkIdExistsOnAllMachines(NetID, true) SetNetworkIdCanMigrate(NetID, false) - SetEntityHeading(Spike, GetEntityHeading(GetPlayerPed(PlayerId()) )) + + SetEntityHeading(Spike, GetEntityHeading(ped)) PlaceObjectOnGroundProperly(Spike) FreezeEntityPosition(Spike, true) + + table.insert(group, { + NetID = NetID, + Coords = GetEntityCoords(Spike) + }) + SpawnCoords = GetOffsetFromEntityInWorldCoords(Spike, 0.0, 4.0, 0.0) - table.insert(SpawnedSpikes, NetID) end + + table.insert(SpawnedSpikes, group) end) --Spike Strip Delete Event RegisterNetEvent('SEM_InteractionMenu:Spikes-DeleteSpikes') AddEventHandler('SEM_InteractionMenu:Spikes-DeleteSpikes', function() - for a = 1, #SpawnedSpikes do - local Spike = NetworkGetEntityFromNetworkId(SpawnedSpikes[a]) - DeleteEntity(Spike) + local ped = PlayerPedId() + local playerCoords = GetEntityCoords(ped) + + local closestIndex = nil + local closestDist = math.huge + + for i = 1, #SpawnedSpikes do + local group = SpawnedSpikes[i] + local coords = group[1].Coords + local dist = #(playerCoords - coords) + + if dist < closestDist then + closestDist = dist + closestIndex = i + end + end + + if not closestIndex or closestDist > 15 then + Notify('~r~No spikes found nearby') + return end - Notify('~r~Spikes Strips Removed!') - SpawnedSpikes = {} + + local group = SpawnedSpikes[closestIndex] + + for i = 1, #group do + local entity = NetworkGetEntityFromNetworkId(group[i].NetID) + if DoesEntityExist(entity) then + DeleteEntity(entity) + end + end + + table.remove(SpawnedSpikes, closestIndex) + + Notify('~r~Removed Spikes') end) --Spike Strip Tire Popping