I'm working on a fivem volleyball script this is my server.lua local objectModel =…
- User
I'm working on a fivem volleyball script this is my server.lua
local objectModel = "prop_beach_volball01" local ball = nil
RegisterCommand('spawnball', function() ball = CreateObject(GetHashKey(objectModel), -1271.1, -1656.22, 3.88, true, false, true) print(ball) print('spawned the ball') end)
How can I activate physics on the ball server side
- Assistant
You can activate the physics on the ball server side by using the
SetObjectPhysicsParamsnative function inside your spawnball command:Here's your updated server.lua:
lualocal objectModel = "prop_beach_volball01" local ball = nil RegisterCommand('spawnball', function() ball = CreateObject(GetHashKey(objectModel), -1271.1, -1656.22, 3.88, true, false, true) SetObjectPhysicsParams(ball, 1000.0, -1.0, 1.0, 1.0, 1.0, 0.97, 0.0, 0.0, 0.0) print(ball) print('spawned the ball and activated physics') end)Explanation for the parameters of the SetObjectPhysicsParams function:
- First parameter is the object itself (ball)
- Second parameter is mass of the object
- Third parameter is gravity (set negative value to enable default gravity)
- Fourth and the rest of the parameters are related to Physics Material (Density, Elasticity, Friction, etc.) Keep these as 1.0, and Friction as 0.97.
You may adjust the values according to your requirement.
- User
are you sure thats not client side function?
- Assistant
You are correct. I apologize for the confusion. The
SetObjectPhysicsParamsfunction is actually a client-side function. In this case, you will need to communicate between the server and the client.Modify your server.lua:
lualocal objectModel = "prop_beach_volball01" local ball = nil RegisterCommand('spawnball', function(source) TriggerClientEvent('spawnBallClient', source, objectModel) print('spawned the ball and activated physics (server-side)') end)Create a new file called client.lua:
luaRegisterNetEvent('spawnBallClient') AddEventHandler('spawnBallClient', function(objectModel) local ball = CreateObject(GetHashKey(objectModel), -1271.1, -1656.22, 3.88, true, false, true) SetObjectPhysicsParams(ball, 1000.0, -1.0, 1.0, 1.0, 1.0, 0.97, 0.0, 0.0, 0.0) end) - User
can i get the server to ApplyForceToEntity
- Assistant
You cannot directly apply force to an entity on the server-side, as physics-related functions are client-side in FiveM. However, you can work around this by using server-to-client communication, similar to the solution provided in my previous response.
Update your server.lua:
lualocal objectModel = "prop_beach_volball01" local ball = nil RegisterCommand('spawnball', function(source) TriggerClientEvent('spawnBallClient', source, objectModel) print('spawned the ball and activated physics (server-side)') end) RegisterCommand('applyforce', function(source, args) local force = {x = tonumber(args[1]), y = tonumber(args[2]), z = tonumber(args[3])} TriggerClientEvent('applyForceClient', source, force) end) - User
is server-to-client communiation better then just doing it on the client and syncing with other clients
- Assistant
It often depends on your specific use case and the intended behavior of your script whether to use server-to-client communication or client-side synchronization. Each approach has its advantages and disadvantages:
-
Server-to-client communication:
- Pros:
- Provides better control over your game logic, allowing you to centralize certain behaviors on the server.
- Easier to manage and validate user inputs, which can be helpful in preventing cheating or abuse.
- Cons:
- Pros:
-