Skip to content

getElementHealth sometimes returns incorrect values for players #486

@ArranTuna

Description

@ArranTuna

Describe the bug
Using the code below (serverside) will set the players health to various values then output getElementHealth both immediately after, and after a 200ms delay. Sometimes, the value returned from getElementHealth does not match the value set 200ms before.

Example output: http://pastebin.com/raw/9VkFAieL
When the player health is set to 83, getElementHealth returns 82 both times. This happens sporadically all through 1-100.

This is a problem is you want to (for example) heal a player 1 health every second, because it will get stuck in a loop doing 82 + 1 forever (example: http://pastebin.com/b103yd0p )

To Reproduce

addCommandHandler("healthcheck", 
	function(player)
		local i = 80

		setElementHealth(player, i)
		outputConsole(string.format("set %.1f", i))
		outputConsole(string.format("get instant %.3f (ceil: %.3f)", getElementHealth(player), math.ceil(getElementHealth(player))))
			
		setTimer(
			function()
				outputConsole(string.format("get delayed %.3f (ceil: %.3f)", getElementHealth(player), math.ceil(getElementHealth(player))))

				i = i + 1

				setElementHealth(player, i)
				outputConsole(string.format("set %.1f", i))
				outputConsole(string.format("get instant %.3f (ceil: %.3f)", getElementHealth(player), math.ceil(getElementHealth(player))))
			end, 
		200, 10)
	end
)

If you run the same code clientside, getElementHealth seems to return the correct values. It also appears to work correctly for vehicles.

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

MTA Client (please complete the following information):
MTA v1.5.3-release-10835

MTA Server (please complete the following information):
MTA v1.5.3-release-10835

Additional context
Using the pastebin code, I found that health wouldn't even increase from 10 up to 11 using v1.5.3-release-10835. Though as it works fine with an increment of 2+ and I doubt many people want to increase health by just 1 and that it can be avoided by using client side setElementHealth, this is a low priority.

BTW you shouldn't be using setElementHealth server side on players as it's so inefficient (it sends a message to every play on the server) instead you triggerClientEvent to that player and have them set their health client side which then updates with other players next pure sync update which is going to be around 100 to 300ms which is fine because remote players don't even need to know exact health of remote players instantly.

From https://bugs.mtasa.com/view.php?id=9492

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsync

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions