I'm working on a fivem volley ball script this is my client.lua local objectnetID = nil…
- User
I'm working on a fivem volley ball script
this is my client.lua local object_netID = nil local objectModel = "prop_beach_volball01" local forwardForceIntensity = 10.0 local upwardForceIntensity = 5.0
local function loadModel(model) print("loading") local modelHash = GetHashKey(model) RequestModel(modelHash)
while not HasModelLoaded(modelHash) do Citizen.Wait(100) end print('done') return modelHashend
local function spawn_the_ball() local player = PlayerPedId() local position = GetEntityCoords(PlayerPedId()) local modelHash = loadModel(objectModel) print("createdobject") createdObject = CreateObjectNoOffset(modelHash, position.x, position.y, position.z + 1, true, false, true) -- Use CreateObjectNoOffset and set isNetwork and netMissionEntity to true ActivatePhysics(createdObject) print("waiting") Citizen.Wait(1000) local playerRotation = GetEntityRotation(PlayerPedId(), 2) local forwardVector = GetEntityForwardVector(PlayerPedId()) local forceVector = vector3( forwardVector.x * forwardForceIntensity, forwardVector.y * forwardForceIntensity, upwardForceIntensity ) ApplyForceToEntity(createdObject, 1, forceVector.x, forceVector.y, forceVector.z, 0.0, 0.0, 0.0, 0, false, true, true, false, true) -- Get the object’s network ID local object_netID = ObjToNet(createdObject) -- Add this line after setting object_netID TriggerServerEvent("VB:setObjectNetworkID", object_netID) end
RegisterCommand("testing", function() spawn_the_ball() end)
local isInGame = true
local function isPlayerNearObject(object, distanceThreshold) local playerCoords = GetEntityCoords(PlayerPedId()) local objCoords = GetEntityCoords(object) local distance = #(playerCoords - objCoords) return distance <= distanceThreshold end
Citizen.CreateThread(function() while isInGame do Citizen.Wait(0)
if object_netID then local createdObject = NetworkGetEntityFromNetworkId(object_netID) if isPlayerNearObject(createdObject, 3.0) and IsControlJustReleased(0, 38) then -- 38 is the key code for "E" print('E pressed') if not NetworkHasControlOfEntity(entity) then print('has no control') NetworkRequestControlOfEntity(entity) while (not NetworkHasControlOfEntity(entity)) do Citizen.Wait(0) end end print('has control') local playerRotation = GetEntityRotation(PlayerPedId(), 2) local forwardVector = GetEntityForwardVector(PlayerPedId()) local forceVector = vector3( forwardVector.x * forwardForceIntensity, forwardVector.y * forwardForceIntensity, upwardForceIntensity ) ApplyForceToEntity(createdObject, 1, forceVector.x, forceVector.y, forceVector.z, 0.0, 0.0, 0.0, 0, false, true, true, false, true) end end endend)
RegisterNetEvent("VB:updateObjectNetworkID") AddEventHandler("VB:updateObjectNetworkID", function(netID) object_netID = netID end)
- Assistant
I see a couple of possible issues in your code, but let me suggest some changes first:
In your client.lua Replace:
local createdObject = NetworkGetEntityFromNetworkId(object_netID)With:
local entity = NetworkGetEntityFromNetworkId(object_netID)And in the same file, replace:
if not NetworkHasControlOfEntity(entity) then print('has no control') NetworkRequestControlOfEntity(entity) while (not NetworkHasControlOfEntity(entity)) do Citizen.Wait(0) end endWith:
if not NetworkHasControlOfEntity(entity) then print('has no control') local timeout = GetGameTimer() + 2000 NetworkRequestControlOfEntity(entity) while (not NetworkHasControlOfEntity(entity)) and (GetGameTimer() < timeout) do Citizen.Wait(0) end end - User
why use local timeout ofver Citizen.Wait()
- Assistant
Using a timeout provides a safeguard to prevent your script from getting stuck in an infinite loop if the desired condition is never met. In this case, the condition we're waiting for is control of the entity using
NetworkHasControlOfEntity(entity). If for some reason the script never gets control, the while loop will still exit after the specified timeout, preventing the script from freezing indefinitely.The
Citizen.Wait(0)inside the loop is still necessary, as it yields the execution of the script and allows other tasks to process in the meantime. Using onlyCitizen.Wait()without a timeout, however, could potentially cause an infinite loop if the condition is never met.By combining
Citizen.Wait(0)with a timeout, you ensure that your script remains responsive to other tasks while also preventing infinite loops.