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 ballend
]] local function loadModel(model) local modelHash = GetHashKey(model) RequestModel(modelHash)
while not HasModelLoaded(modelHash) do Citizen.Wait(0) end return modelHashend --[[
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 can I make it so the volley ball goes towards the other player lets say player id 2
- Assistant
In order to make the volleyball go towards another player with a specified ID, you can follow these steps:
1- Get the player's server ID. 2- Translate the server ID to a ped (network) ID in the client script. 3- Get the target player's coords. 4- Calculate the direction vector between the two positions. 5- Apply the force based on the direction vector.
Here's the updated script:
local function getTargetPlayerCoords(targetServerId) local targetPlayer = GetPlayerFromServerId(targetServerId) if targetPlayer ~= -1 then local targetPedId = GetPlayerPed(targetPlayer) return GetEntityCoords(targetPedId) end end local function spawn_volleyball_towards_target(targetServerId) local player = PlayerPedId() local position = GetEntityCoords(player) local targetCoords = getTargetPlayerCoords(targetServerId) if targetCoords then 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 direction = (targetCoords - position):normalize() local upwardForce = vector3(0, 0, upwardForceIntensity) local forwardForce = vector3( direction.x * forwardForceIntensity, direction.y * forwardForceIntensity, 0 ) local forceVector = forwardForce + upwardForce ApplyForceToEntity(createdObject, 1, forceVector.x, forceVector.y, forceVector.z, 0.0, 0.0, 0.0, 0, false, true, true, false, true) else print("Target player not found") end end RegisterCommand("testing", function(_, args) local targetServerId = tonumber(args[1]) if targetServerId and targetServerId > 0 then spawn_volleyball_towards_target(targetServerId) else print("Invalid player ID") end end)Now, to make the volleyball go towards the other player, you can just type
/testing <player_id>in the chat, replacing<player_id>with the other player's server ID (e.g.,/testing 2).