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)
RegisterServerEvent("VB:requestObjectNetworkID") AddEventHandler("VB:requestObjectNetworkID", function() local src = source if object_netID then TriggerClientEvent("VB:updateObjectNetworkID", src, object_netID) end end)
currently the volleyball will spawn with the /testing command the client that spawned the ball can interacting with it normally IE ApplyForceToEntity but the other client the ball will move for a second on there screen but then tp back to its last position and the orignal client who spawned the object can't see the small glitch
- 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) endThis should create the object as a network object so that the interactions done by one client are properly synced and visible to other clients.
- User
it still does not let the other clients interact with the object
- Assistant
I apologize for the confusion. After reviewing your script, it seems that the issue is related to setting the object's network ID. I believe this is because the object's network ID is not being set correctly on the server.
To fix this, we're going to make a couple of changes. First, we'll add a
NetworkRegisterEntityAsNetworkedcall inclient.lua, and we'll move the server events fromserver.luatoclient.lua. This should fix the synchronization issue for other clients.Here are the updated files:
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 modelHash end 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) -- Add this line NetworkRegisterEntityAsNetworked(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) -- Remove the following two lines from this file and add them in server.lua -- TriggerClientEvent("VB:updateObjectNetworkID", -1, netID) -- TriggerClientEvent("VB:updateObjectNetworkID", src, 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 end end) RegisterNetEvent("VB:updateObjectNetworkID") AddEventHandler("VB:updateObjectNetworkID", function(netID) object_netID = netID end) Citizen.CreateThread(function() TriggerServerEvent("VB:requestObjectNetworkID") end) -- Add this event to spawn the object on other clients whenever it is spawned RegisterNetEvent("VB:spawnObjectOnClients") AddEventHandler("VB:spawnObjectOnClients", function(coords, netID) 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)server.lua:
local object_netID = nil RegisterServerEvent("VB:setObjectNetworkID") AddEventHandler("VB:setObjectNetworkID", function(netID) object_netID = netID TriggerClientEvent("VB:updateObjectNetworkID", -1, netID) -- Add this line to send the coordinates and netID to all clients TriggerClientEvent("VB:spawnObjectOnClients", -1, GetEntityCoords(NetworkGetEntityFromNetworkId(netID)), netID) end) RegisterServerEvent("VB:requestObjectNetworkID") AddEventHandler("VB:requestObjectNetworkID", function() local src = source if object_netID then TriggerClientEvent("VB:updateObjectNetworkID", src, object_netID) end end)After making these changes, when a client spawns the ball, it should spawn for all clients, and all clients should be able to interact with it.
- 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)server.lua:
-- Modify the "VB:setObjectNetworkID" event handler to receive the source client ID RegisterServerEvent("VB:setObjectNetworkID") AddEventHandler("VB:setObjectNetworkID", function(netID, src) object_netID = netID TriggerClientEvent("VB:updateObjectNetworkID", -1, netID) -- Add the client ID as a parameter to the "VB:spawnObjectOnClients" event TriggerClientEvent("VB:spawnObjectOnClients", -1, GetEntityCoords(NetworkGetEntityFromNetworkId(netID)), netID, src) end)With these changes, the object should be spawned only once for each client, and all clients should be able to interact with it.
- User
It spawns one object then spawns 2 more after around a second. only the client that spawned the object can interact with it. the other client can interact but it doesn't server sync and then syncs the client up with whoever spawned the object
client.lua
local QBCore = exports['qb-core']:GetCoreObject() 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 SetEntityDynamic(createdObject, true) -- Add this line NetworkRegisterEntityAsNetworked(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, GetPlayerServerId(PlayerId())) -- Remove the following two lines from this file and add them in server.lua -- TriggerClientEvent("VB:updateObjectNetworkID", -1, netID) --TriggerClientEvent("VB:updateObjectNetworkID", src, 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)
-- Add this event to spawn the object on other clients whenever it is spawned RegisterNetEvent("VB:spawnObjectOnClients") AddEventHandler("VB:spawnObjectOnClients", function(coords, netID) 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)
server.lua
local QBCore = exports['qb-core']:GetCoreObject() local object_netID = nil
-- Modify the "VB:setObjectNetworkID" event handler to receive the source client ID RegisterServerEvent("VB:setObjectNetworkID") AddEventHandler("VB:setObjectNetworkID", function(netID, src) object_netID = netID TriggerClientEvent("VB:updateObjectNetworkID", -1, netID) -- Add the client ID as a parameter to the "VB:spawnObjectOnClients" event TriggerClientEvent("VB:spawnObjectOnClients", -1, GetEntityCoords(NetworkGetEntityFromNetworkId(netID)), netID, src) end)
RegisterServerEvent("VB:requestObjectNetworkID") AddEventHandler("VB:requestObjectNetworkID", function() local src = source if object_netID then TriggerClientEvent("VB:updateObjectNetworkID", src, object_netID) end end)
- 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)Now, the object's network ID should be sent only to the client that created the object, preventing it from spawning multiple times.
- 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.
- Only send the object's network ID to other clients when it's spawned and store it for interaction.
Here's the updated implementation:
client.lua:
-- Remove "VB:spawnObjectOnClients" event block --[[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)]]-- -- Reinstate the "VB:requestObjectNetworkID" in the client-side script Citizen.CreateThread(function() TriggerServerEvent("VB:requestObjectNetworkID") end)server.lua:
-- Remove "VB:spawnObjectOnClients" event trigger line from "VB:setObjectNetworkID" event RegisterServerEvent("VB:setObjectNetworkID") AddEventHandler("VB:setObjectNetworkID", function(netID, src) object_netID = netID TriggerClientEvent("VB:updateObjectNetworkID", -1, netID) -- Send the netID to all clients --TriggerClientEvent("VB:spawnObjectOnClients", -1, GetEntityCoords(NetworkGetEntityFromNetworkId(netID)), netID, src) -- Remove this line end)After making these changes, only the client that spawns the object should create the object, but all clients should be able to interact with it. Let me know if you still encounter any issues.
- User
the object only spawns once now but only the client that spawned the object can interact with it
this is my current code
sever.lua
local QBCore = exports['qb-core']:GetCoreObject() local object_netID = nil
-- Modify the "VB:setObjectNetworkID" event handler to receive the source client ID -- 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 end)
RegisterServerEvent("VB:requestObjectNetworkID") AddEventHandler("VB:requestObjectNetworkID", function() local src = source if object_netID then TriggerClientEvent("VB:updateObjectNetworkID", src, object_netID) end end)
client.lua
local QBCore = exports['qb-core']:GetCoreObject() 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 SetEntityDynamic(createdObject, true) -- Add this line NetworkRegisterEntityAsNetworked(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, GetPlayerServerId(PlayerId())) -- Remove the following two lines from this file and add them in server.lua -- TriggerClientEvent("VB:updateObjectNetworkID", -1, netID) --TriggerClientEvent("VB:updateObjectNetworkID", src, 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)
-- Reinstate the “VB:requestObjectNetworkID” in the client-side script Citizen.CreateThread(function() TriggerServerEvent("VB:requestObjectNetworkID") end)
- 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:
-- Add the interaction synchronization event RegisterServerEvent("VB:interactBall") AddEventHandler("VB:interactBall", function(coords, forceVector) TriggerClientEvent("VB:applyForceToBall", -1, coords, forceVector, source) end)client.lua:
-- Add the interaction synchronization 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) ApplyForceToEntity(createdObject, 1, forceVector.x, forceVector.y, forceVector.z, 0.0, 0.0, 0.0, 0, false, true, true, false, true) end 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 ) ApplyForceToEntity(createdObject, 1, forceVector.x, forceVector.y, forceVector.z, 0.0, 0.0, 0.0, 0, false, true, true, false, true) TriggerServerEvent("VB:interactBall", GetEntityCoords(createdObject), forceVector) -- Send the interaction event to the server end endBy making these changes, when a client interacts with the ball, it sends an event to the server with the interaction's location and force vector. The server then broadcasts the event to all clients, so they apply the same force to the object.
Now, other clients should be able to interact with the object, and it should synchronize correctly.
- 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)This change should now let other clients interact with the object and apply force to it as expected.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.