I'm working on a fivem volleyball script client.lua local objectnetID = nil local…
- User
I'm working on a fivem volleyball script
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 = CreateObject(modelHash, position.x, position.y, position.z + 1, true, false, true) SetEntityDynamic(createdObject, true) 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" 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)
Citizen.CreateThread(function() TriggerServerEvent("VB:requestObjectNetworkID") end)
server.lua
local QBCore = exports['qb-core']:GetCoreObject() local object_netID = nil
RegisterServerEvent("VB:setObjectNetworkID") AddEventHandler("VB:setObjectNetworkID", function(netID) object_netID = netID TriggerClientEvent("VB:updateObjectNetworkID", -1, netID) end)
- Assistant
This seems to be an issue with synchronization between the clients. The object should be created using the
CreateObjectNoOffsetand marked as a network object by passing true forisNetworkandnetMissionEntityparameters. Below are the changes that should be made:In client.lua, modify the
spawn_the_ballfunction: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 SetEntityDynamic(createdObject, true) 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 - User
it still does not let the other clients interact with the object
- Assistant
- User
its created 3 objects for some reason
- Assistant
Apologies for the oversight. The event "VB:spawnObjectOnClients" should only spawn the object on other clients and not on the client that already spawned it. To avoid this, you can pass the source ID of the client that spawned the object to the server and then exclude that client when triggering the event. Here's how to modify the server and client scripts:
client.lua:
-- Modify the TriggerServerEvent call in the spawn_the_ball function local function spawn_the_ball() -- ... TriggerServerEvent("VB:setObjectNetworkID", object_netID, GetPlayerServerId(PlayerId())) -- ... end -- Modify the "VB:spawnObjectOnClients" event handler RegisterNetEvent("VB:spawnObjectOnClients") AddEventHandler("VB:spawnObjectOnClients", function(coords, netID, src) if GetPlayerServerId(PlayerId()) ~= src then local modelHash = GetHashKey(objectModel) RequestModel(modelHash) while not HasModelLoaded(modelHash) do Citizen.Wait(0) end createdObject = CreateObjectNoOffset(modelHash, coords.x, coords.y, coords.z + 1, true, false, true) SetEntityDynamic(createdObject, true) NetworkRegisterEntityAsNetworked(createdObject) object_netID = netID end end) - User
- Assistant
I see that the
srcparameter was missing in the "VB:spawnObjectOnClients" event handler in client.lua. I apologize for this mistake, and I appreciate your patience. Here are the updated changes that should resolve the issues:client.lua:
-- Add 'src' parameter in "VB:spawnObjectOnClients" event handler RegisterNetEvent("VB:spawnObjectOnClients") AddEventHandler("VB:spawnObjectOnClients", function(coords, netID, src) if GetPlayerServerId(PlayerId()) ~= src then local modelHash = GetHashKey(objectModel) RequestModel(modelHash) while not HasModelLoaded(modelHash) do Citizen.Wait(0) end createdObject = CreateObjectNoOffset(modelHash, coords.x, coords.y, coords.z + 1, true, false, true) SetEntityDynamic(createdObject, true) NetworkRegisterEntityAsNetworked(createdObject) object_netID = netID end end)After making these changes, the object should be spawned only once for each client, and all clients should be able to interact with it. Please let me know if you have any further issues or if the problem persists.
- User
it still spawns the main object on /testing but after a second spawns another object it is down from 3 to 2 tho so thats some progress
- Assistant
I believe I found the remaining issue. The cause is the
TriggerServerEvent("VB:requestObjectNetworkID")inside the client-side script.When the object is spawned, the
VB:requestObjectNetworkIDevent is triggered, which sends the object's network ID back to the client that requested it. The issue is that not only the client that spawned the object but all clients will store the object's network ID, causing it to spawn multiple times.To fix this, we'll modify the server-side script and remove the
TriggerServerEvent("VB:requestObjectNetworkID")from the client-side script:client.lua:
-- Remove this block --[[Citizen.CreateThread(function() TriggerServerEvent("VB:requestObjectNetworkID") end)]]--server.lua:
-- Update the "VB:setObjectNetworkID" event handler RegisterServerEvent("VB:setObjectNetworkID") AddEventHandler("VB:setObjectNetworkID", function(netID, src) object_netID = netID TriggerClientEvent("VB:updateObjectNetworkID", src, netID) -- Update this line to send the netID only to the client that created the object TriggerClientEvent("VB:spawnObjectOnClients", -1, GetEntityCoords(NetworkGetEntityFromNetworkId(netID)), netID, src) end) - User
its still spawning two objects
I think it is because of this event RegisterNetEvent("VB:spawnObjectOnClients") AddEventHandler("VB:spawnObjectOnClients", function(coords, netID, src) if GetPlayerServerId(PlayerId()) ~= src then local modelHash = GetHashKey(objectModel) RequestModel(modelHash) while not HasModelLoaded(modelHash) do Citizen.Wait(0) end createdObject = CreateObjectNoOffset(modelHash, coords.x, coords.y, coords.z + 1, true, false, true) SetEntityDynamic(createdObject, true) NetworkRegisterEntityAsNetworked(createdObject) object_netID = netID end end) as a object doesn't need to be spawned for both clients as they can both see it if only one client creates the object but only one can interact with the object
- Assistant
I apologize for the confusion. You're correct, it would be best to only synchronize the object's interaction but not spawn it multiple times for each client.
Given the information from the previous discussions, I think the best approach is to:
- Remove the "VB:spawnObjectOnClients" event from the client-side and server-side scripts.
- Keep the "VB:requestObjectNetworkID" in the client-side script.
- User
- Assistant
Thank you for the clarification. In that case, we can attempt to synchronize the interactions by sending an event from the client to the server and then back to other clients when they interact with the object.
Update server.lua and client.lua with this code:
server.lua:
- User
when another client presses E nothing happens a force is only applied to the entity when the client that spawned the object presses E
- Assistant
My apologies for the confusion. I see the issue now - the force is applied twice to the object on the client that spawned it because we kept the
ApplyForceToEntity()in the loop while we added the synchronization code. Let's refactor the interaction code.Create a function named 'applyForceToObject' in client.lua and refactor the mentioned loop:
client.lua:
-- New function to apply force to the object local function applyForceToObject(object, x, y, z) ApplyForceToEntity(object, 1, x, y, z, 0.0, 0.0, 0.0, 0, false, true, true, false, true) end -- Replace the interaction code in the Citizen.CreateThread while loop 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" local playerRotation = GetEntityRotation(PlayerPedId(), 2) local forwardVector = GetEntityForwardVector(PlayerPedId()) local forceVector = vector3( forwardVector.x * forwardForceIntensity, forwardVector.y * forwardForceIntensity, upwardForceIntensity ) applyForceToObject(createdObject, forceVector.x, forceVector.y, forceVector.z) TriggerServerEvent("VB:interactBall", GetEntityCoords(createdObject), forceVector) -- Send the interaction event to the server end end -- Update the "VB:applyForceToBall" event handler RegisterNetEvent("VB:applyForceToBall") AddEventHandler("VB:applyForceToBall", function(coords, forceVector, src) if GetPlayerServerId(PlayerId()) ~= src and object_netID then local createdObject = NetworkGetEntityFromNetworkId(object_netID) applyForceToObject(createdObject, forceVector.x, forceVector.y, forceVector.z) end end)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.