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
- 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) - 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:
- In the server-side script