I'm working on a fivem volleyball script server.lua Citizen.CreateThread(function()…
- 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 endend)
how can I get this working as the GetGroundZFor_3dCoord is a client event
- Assistant
You can trigger a client event from the server side by using the
TriggerClientEventfunction. To do this, you'll need to create a function on the client side that will handle the server's request and execute theGetGroundZFor_3dCoordfunction. 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
getGroundZevent, it will pass the volleyball coordinates to the client. The client will then execute theGetGroundZFor_3dCoordfunction with the given coordinates and send the result back to the server using thereceiveGroundZevent. 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.