Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetEntityHealth returns different value between server/client #2558

Closed
nullvariable opened this issue May 23, 2024 · 7 comments
Closed

GetEntityHealth returns different value between server/client #2558

nullvariable opened this issue May 23, 2024 · 7 comments
Labels
bug RedM Issues/PRs related to RedM

Comments

@nullvariable
Copy link

nullvariable commented May 23, 2024

What happened?

GetEntityHealth values between client and server do not match expectations

Expected result

GetEntityHealth would report the same value on the server as the client

Reproduction steps

on client

RegisterCommand('high_health', function()
  local ped = PlayerPedId()
  SetPedMaxHealth(ped, 1100)
  ReviveInjuredPed(ped)
  SetEntityHealth(ped, 1100)
  Wait(100)
  print(GetEntityHealth(ped)) -- result: 1100
  print(GetEntityMaxHealth(ped)) -- result: 1100

on server

RegisterCommand('test', function(src)
  local ped = GetPlayerPed(src)
  print(GetEntityHealth(ped)) -- result: 200
  print(GetEntityMaxHealth(ped)) -- result: 200
  print(GetPedMaxHealth(ped)) -- result: 200
  print(GetPlayerMaxHealth(src)) -- result: 1100

Setting values below 200 returns the same value on both client and server.

Importancy

There's a workaround

Area(s)

FiveM, RedM, FXServer

Specific version(s)

FiveM/b3095 - FXServer 8049 win32

Additional information

with the GetPlayerMaxHealth function I somewhat expected a similar function for getting health if the server was going to differ at all.

In examining the CPedHealthDataNode definition, it looks like if the data for maxhealth is 0 it defaults to 200, but this was the only instance I could track down where that value might be coming from.

int maxHealth = (data.maxHealth == 0) ? 200 : data.maxHealth;

I would like to sample the correct value on the server side either via a new GetPlayerHealth function, or preferably via the GetEntityHealth function.

@nullvariable nullvariable added bug triage Needs a preliminary assessment to determine the urgency and required action labels May 23, 2024
@github-actions github-actions bot added the RedM Issues/PRs related to RedM label May 23, 2024
@AvarianKnight
Copy link
Contributor

Can you verify that the same thing happens for non-player peds?

@Pedro-Lucas14
Copy link

Pedro-Lucas14 commented May 23, 2024

you can use this Thread to place MaxHealth

CreateThread(function()
	while true do
 		local Ped = PlayerPedId()
		if GetEntityMaxHealth(Ped) ~= 401 then
			SetEntityMaxHealth(Ped,401)
			SetPedMaxHealth(Ped,401)
		end
		Wait(1000)
 	end
 end)

@tens0rfl0w
Copy link
Contributor

tens0rfl0w commented May 23, 2024

You need to use SET_ENTITY_MAX_HEALTH to populate the CPedHealthDataNode, using other natives to set the max health of a player-ped will only populate the CPlayerGameStateDataNode.

The same applies for non-player peds.

@nullvariable
Copy link
Author

adding SetEntityMaxHealth doesn't seem to change the outcome. Testing on a server spawned ped appears to return the same results where clientside the value of the ped health reports the desired max and full values, but the server side still only reports 200.

server.lua:

RegisterCommand('create_test_ped', function(src)
    local coords = GetEntityCoords(GetPlayerPed(src)) + vector3(2, 2, 1)
    local ped = CreatePed(4, `s_m_y_cop_01`, coords.x, coords.y, coords.z, 0.0, true, true)
    while not DoesEntityExist(ped) do Wait(0) end
    print(NetworkGetNetworkIdFromEntity(ped))
    print(GetEntityHealth(ped))
    print(GetEntityMaxHealth(ped))
end)

RegisterCommand('report_ped_health', function(_, args)
    local ped = NetworkGetEntityFromNetworkId(tonumber(args[1]))
    print(GetEntityHealth(ped))
    print(GetEntityMaxHealth(ped))
    print(GetPedMaxHealth(ped))
end)

client.lua

RegisterCommand('set_test_ped_health', function(_, args)
    local ped = NetworkGetEntityFromNetworkId(tonumber(args[1]))
    SetPedMaxHealth(ped, 1100)
    SetEntityMaxHealth(ped, 1100)
    ReviveInjuredPed(ped)
    Wait(0)
    SetEntityHealth(ped, 1100)
    print(GetEntityHealth(ped))
    print(GetEntityMaxHealth(ped))
end)

results:

> create_test_ped
65534
0
0
> report_ped_health 65534
200
200
200
> set_test_ped_health 65534
1100
1100
> report_ped_health 65534
200
200
200

@nullvariable
Copy link
Author

I tested a couple of other things.
using the test ped above, client A sets the higher values, which read back as expected on Client A. But Client B reports the normal 200/200 values while the ped is owned by Client A, regardless if the values are set while client B is present or arrives after.

Client A leaving the area and Client B setting health, Client A returning reports 200/200

Client A and B both report the expected elevated values for their player peds on the clientside in all scenarios though

@tens0rfl0w
Copy link
Contributor

tens0rfl0w commented May 23, 2024

You shouldn't call SET_PED_MAX_HEALTH and only use SET_ENTITY_MAX_HEALTH as calling SetPedMaxHealth before SetEntityMaxHealth with the same value causes game code to not set the change bit on the sync node as local max health hasn't changed. (Only SetEntityMaxHealth sets the bit flag and only if local max health was altered by the script command, which isn't the case here as being previously set to the same value by SetPedMaxHealth)

(You can easily test this with calling SetPedMaxHealth before SetEntityMaxHealth but with a different value.)

@nullvariable
Copy link
Author

That makes sense, and removing the SET_PED_MAX_HEALTH call from my code as well as the examples outlined above creates the desired result for me.

Thanks!

@github-actions github-actions bot removed the triage Needs a preliminary assessment to determine the urgency and required action label May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug RedM Issues/PRs related to RedM
Projects
None yet
Development

No branches or pull requests

4 participants