Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

fivem script how to detect serverside if an object hits the ground

  1. User

    fivem script how to detect serverside if an object hits the ground

  2. Assistant

    To detect when an object hits the ground in a FiveM server-side script, you can use collision events and the GetEntityHeightAboveGround native function. Here's an example:

    RegisterServerEvent("detectObjectHitGround")
    AddEventHandler("detectObjectHitGround", function(objectNetID)
        print("Object [" .. objectNetID .. "] hit the ground")
    
        -- You can perform any action related to the object hitting the ground.
        -- For example, delete the object:
        obj = NetworkGetEntityFromNetworkId(objectNetID)
        if DoesEntityExist(obj) then
            DeleteEntity(obj)
        end
    end)
    

    This script registers a server event that will be triggered when an object hits the ground. To track the object's position and detect if it hit the ground, you should monitor it in a client-side script.

    Here's an example of a client-side script that continuously checks if the object has hit the ground and triggers the server event:

    Citizen.CreateThread(function()
        local object = -- The object you want to monitor
        local objectNetID = NetworkGetNetworkIdFromEntity(object)
    
        while true do
            Citizen.Wait(50)
    
            local heightAboveGround = GetEntityHeightAboveGround(object)
    
            if heightAboveGround <= 0.5 then  -- The threshold to consider the object has hit the ground.
                TriggerServerEvent("detectObjectHitGround", objectNetID)
                break
            end
        end
    end)
    

    This script runs in a loop, checking the object's height above ground every 50 milliseconds. If the object's height is below 0.5 (or any other threshold you choose), it triggers the server event. You can adjust the frequency of the checks and the threshold according to your needs.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.