I'm working on a fivem volleyball script written in lua this is my code local QBCore =…
- User
I'm working on a fivem volleyball script written in lua this is my 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 if the variable in-game is true it keeps checking until in-game is no longer true if your near the ball and if so then you can press E and it will launch the ball forwards the direction your looking
- Assistant
You can achieve this by adding a while loop and checking for the condition inside the loop. In this case, you want to check if the in-game variable is true, and if the player is near the object, they can press "E" to launch the ball.
To implement this, follow these steps:
- Create a new variable at the top of the file:
local isInGame = trueThis will act as the in-game variable you mentioned.
- Add a function to check if the player is near the object:
local function isPlayerNearObject(object, distanceThreshold) local playerCoords = GetEntityCoords(PlayerPedId()) local objCoords = GetEntityCoords(object) local distance = #(playerCoords - objCoords) return distance <= distanceThreshold end