I’m working on a fivem volleyball script this is my client code local QBCore =…
- User
I’m working on a fivem volleyball script
this is my client code
local QBCore = exports[‘qb-core’]:GetCoreObject() local objectModel = “prop_beach_volball01” local createdObject = “” local zoffset = 2 local createdObject = 0 –[[
function LaunchBall(ball, force) print(‘trying to lauch the ball’) local x, y, z = table.unpack(GetEntityCoords(ball, true)) ApplyForceToEntity(ball, 1, 0.0, 0.0, force, 0.0, 0.0, 0.0, true, true, true, true, true) end
function SpawnVolleyball() local ballHash = GetHashKey(“prop_beach_volball01”) RequestModel(ballHash) while not HasModelLoaded(ballHash) do Citizen.Wait(100) end
local x, y, z = table.unpack(GetEntityCoords(PlayerPedId())) local ball = CreateObject(ballHash, x + 1, y, z, true, true, false) SetDamping(ball, 2, 0.1) PlaceObjectOnGroundProperly(ball) Citizen.Wait(1000) LaunchBall(ball, 10) return ball end
]] local function loadModel(model) local modelHash = GetHashKey(model) RequestModel(modelHash)
while not HasModelLoaded(modelHash) do Citizen.Wait(0) end
return modelHash end –[[
local function spawnball(hash) local position = GetEntityCoords(PlayerPedId()) – You can change this to any other coordinates createdObject = CreateObject(hash, position.x, position.y, position.z + zoffset, true, true, true) end
RegisterCommand(“testing”, function() print(‘creating object’) local modelHash = loadModel(objectModel) spawnball(modelHash) end)
RegisterCommand(“applyphy”, function() print(‘applyphy’) SetEntityDynamic(createdObject, true) – This ensures that the object’s physics are applied ApplyForceToEntity(createdObject, 1, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, false, true, true, false, true) end)
]] local upwardForceIntensity = 150.0 local forwardForceIntensity = 200.0 –local upwardForceIntensity = 150.0 –local forwardForceIntensity = 200.0 local upwardForceIntensity = 30.0 local forwardForceIntensity = 20.0
local function spawn_the_ball() local player = PlayerPedId() local position = GetEntityCoords(PlayerPedId()) local modelHash = loadModel(objectModel) 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) end
RegisterCommand(“testing”, function() spawn_the_ball() end)
how do i got about server syncing the ball so all players can see it and see when its interacted with also add a commahnd that launches the ball vertical and syncs that between all clients
- Assistant
To sync the volleyball between all clients on the server, you could create a networked entity and then use server-side events to synchronize any actions or updates made to the volleyball. Here's a potential implementation:
- Create a server-side script
server.luaand add it to yourfxmanifest.lua:
fx_version 'bodacious' game 'gta5' client_scripts { 'client.lua', } server_scripts { 'server.lua', }- In your server-side script,
server.lua, create two server-side events:
RegisterNetEvent("Volleyball:Spawn") AddEventHandler("Volleyball:Spawn", function() local src = source TriggerClientEvent("Volleyball:SpawnForAll", -1, src) end) RegisterNetEvent("Volleyball:ApplyForce") AddEventHandler("Volleyball:ApplyForce", function(forceVector) local src = source TriggerClientEvent("Volleyball:ApplyForceForAll", -1, forceVector, src) end)- In your client-side script,
client.lua, you need to do the following changes:
- Replace your
spawn_the_ballfunction with a new function that listens to the server event for spawning:
local function create_networked_ball(player) local position = GetEntityCoords(player) local modelHash = loadModel(objectModel) createdObject = CreateObject(modelHash, position.x, position.y, position.z + 1, true, false, true, true) SetEntityDynamic(createdObject, true) end RegisterNetEvent("Volleyball:SpawnForAll") AddEventHandler("Volleyball:SpawnForAll", function(playerId) local player = GetPlayerFromServerId(playerId) create_networked_ball(player) end)- Replace your
testingcommand with one that triggers the server event for spawning:
RegisterCommand("testing", function() TriggerServerEvent("Volleyball:Spawn") end)- Create a new command to launch the ball vertically and trigger the server event for applying force:
RegisterCommand("launchball", function() local forceVector = vector3(0.0, 0.0, upwardForceIntensity) TriggerServerEvent("Volleyball:ApplyForce", forceVector) end)- Add an event handler for applying force to the volleyball:
RegisterNetEvent("Volleyball:ApplyForceForAll") AddEventHandler("Volleyball:ApplyForceForAll", function(forceVector, playerId) if createdObject ~= 0 then ApplyForceToEntity(createdObject, 1, forceVector.x, forceVector.y, forceVector.z, 0.0, 0.0, 0.0, 0, false, true, true, false, true) end end)With these changes, the volleyball should be synced between all players on the server. The
testingcommand will spawn the volleyball and thelaunchballcommand will launch the ball vertically. Both actions will be synced between all clients. - Create a server-side script
- User
local function create_networked_ball(player) print("Player") print(player) local position = GetEntityCoords(player) print("pos") print(position) local modelHash = loadModel(objectModel) createdObject = CreateObject(modelHash, position.x, position.y, position.z + 1, true, false, true, true) SetEntityDynamic(createdObject, true) end RegisterNetEvent("Volleyball:SpawnForAll") AddEventHandler("Volleyball:SpawnForAll", function(playerId) local player = GetPlayerFromServerId(playerId) create_networked_ball(player) end)
the prints I get for pos vector3 is vector3(0, 0, 0) and the print I get for Player is different for each client
- Assistant
The problem is happening because
GetPlayerFromServerIdis getting the player index for the local player, not the target player that spawned the ball. Instead, useGetEntityCoordswith the playerId received as a network ID to get the correct player's location.Update the
Volleyball:SpawnForAllevent handler in your client-side scriptclient.luaas follows:RegisterNetEvent("Volleyball:SpawnForAll") AddEventHandler("Volleyball:SpawnForAll", function(playerId) local player = NetworkGetEntityFromNetworkId(playerId) create_networked_ball(player) end)Update the
Volleyball:Spawnevent in your server-side scriptserver.luato send the network ID:RegisterNetEvent("Volleyball:Spawn") AddEventHandler("Volleyball:Spawn", function() local src = source local srcPlayer = GetPlayerPed(src) local networkId = PedToNet(srcPlayer) TriggerClientEvent("Volleyball:SpawnForAll", -1, networkId) end)With these changes, the ball should spawn at the correct player's location on all clients.
- User
RegisterNetEvent("Volleyball:Spawn") AddEventHandler("Volleyball:Spawn", function() local src = source local srcPlayer = GetPlayerPed(src) local networkId = PedToNet(srcPlayer) TriggerClientEvent("Volleyball:SpawnForAll", -1, networkId) end)
I'm getting this error
[ script:qb-jimmy] SCRIPT ERROR: @qb-jimmy/server/main.lua:8: attempt to call a nil value (global 'PedToNet') [ script:qb-jimmy] > handler (@qb-jimmy/server/main.lua:8)
- Assistant
Apologies for the confusion. I made a mistake in the previous response. The
PedToNetfunction is a client-side function, so it cannot be used on the server-side script. To fix this issue, you can send the source (serverId) and convert it to the networkId on the client-side.Follow these steps to make the necessary changes:
- Update the
Volleyball:Spawnevent in your server-side scriptserver.luato send only the source (serverId):
RegisterNetEvent("Volleyball:Spawn") AddEventHandler("Volleyball:Spawn", function() local src = source TriggerClientEvent("Volleyball:SpawnForAll", -1, src) end)- Update the
Volleyball:SpawnForAllevent handler in your client-side scriptclient.luato convert the serverId to a network ID:
RegisterNetEvent("Volleyball:SpawnForAll") AddEventHandler("Volleyball:SpawnForAll", function(serverId) local playerPed = GetPlayerPed(GetPlayerFromServerId(serverId)) local networkId = PedToNet(playerPed) local player = NetworkGetEntityFromNetworkId(networkId) create_networked_ball(player) end)Now the script should work correctly, spawning the ball at the right player's location on all clients.
- Update the
- User
it now spawns the prop multiple times depending on how many people are on the server
all i want is for one ball to spawn but for everyone to be able to ApplyForceToEntity etc
- Assistant
I understand, let's make the necessary modifications to spawn only one ball and allow everyone to apply force to it.
- In the server-side script
server.lua, create a globalspawnedvariable to store whether the ball has been spawned or not:
local spawned = false RegisterNetEvent("Volleyball:Spawn") AddEventHandler("Volleyball:Spawn", function() if not spawned then spawned = true local src = source TriggerClientEvent("Volleyball:SpawnForAll", -1, src) end end) RegisterNetEvent("Volleyball:RemoveSpawnedFlag") AddEventHandler("Volleyball:RemoveSpawnedFlag", function() spawned = false end)- In the client-side script
client.lua, add a command to delete the ball and reset the spawned state on the server:
RegisterCommand("deleteball", function() if createdObject ~= 0 then DeleteEntity(createdObject) createdObject = 0 TriggerServerEvent("Volleyball:RemoveSpawnedFlag") end end)Now, the ball will only be spawned once, and everyone can apply force to it. You can use the
deleteballcommand to delete the current ball and allow for a new one to be spawned with thetestingcommand.Keep in mind that this implementation has some limitations. For instance, if the player who initially spawned the ball leaves the server, the ball will remain for other players but won't be deletable with the
deleteballcommand. To work around this, you could implement a more advanced logic, such as tracking the player who created the ball and allowing only this player to delete it or assign a new "owner" when the creator leaves. - In the server-side script
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.