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

I'm working on a fivem volleyball script server.lua Citizen.CreateThread(function()…

  1. User

    I'm working on a fivem volleyball script server.lua

    Citizen.CreateThread(function() while true do Citizen.Wait(0) print('by') local createdObject = NetworkGetEntityFromNetworkId(object_netID) local volleyballCoords = GetEntityCoords(createdObject) local _, groundZ = GetGroundZFor_3dCoord(volleyballCoords.x, volleyballCoords.y, volleyballCoords.z) local isColliding = HasEntityCollidedWithAnything(createdObject)

        if isColliding and (volleyballCoords.z - groundZ) <= groundThreshold then
            -- The volleyball has hit the ground
            print('is on the ground')
        end
    end
    

    end)

    how can I get this working as the GetGroundZFor_3dCoord is a client event

  2. Assistant

    You can trigger a client event from the server side by using the TriggerClientEvent function. To do this, you'll need to create a function on the client side that will handle the server's request and execute the GetGroundZFor_3dCoord function. Here's an example of how you can achieve this:

    Create a new file called client.lua and add the following code:

    RegisterNetEvent('getGroundZ')
    AddEventHandler('getGroundZ', function(coords)
    
        local _, groundZ = GetGroundZFor_3dCoord(coords.x, coords.y, coords.z)
    
        -- Send the groundZ value back to the server
        TriggerServerEvent('receiveGroundZ', groundZ)
    end)
    

    Then modify your server.lua file to use the new event like this:

    RegisterNetEvent('receiveGroundZ')
    AddEventHandler('receiveGroundZ', function(groundZ)
        -- You can now use the received groundZ value in your server-side code
        print("Received groundZ: "..groundZ)
    end)
    
    Citizen.CreateThread(function()
        while true do
            Citizen.Wait(0)
            print('by')
            local createdObject = NetworkGetEntityFromNetworkId(object_netID)
            local volleyballCoords = GetEntityCoords(createdObject)
            local isColliding = HasEntityCollidedWithAnything(createdObject)
    
            -- Trigger the client event to get the groundZ value
            TriggerClientEvent('getGroundZ', -1, volleyballCoords)
    
            Citizen.Wait(1000) -- Wait for 1 second to allow the client event to execute and return the groundZ value
                                
            -- Compare groundZ value and volleyball coords in the receiveGroundZ event handler above
            
        end
    end)
    

    Now, when the server triggers the getGroundZ event, it will pass the volleyball coordinates to the client. The client will then execute the GetGroundZFor_3dCoord function with the given coordinates and send the result back to the server using the receiveGroundZ event. The server can then use the returned groundZ value in its calculations. Make sure to include both client.lua and server.lua in your __resource.lua file.