• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Setting max health?

Evan

A splendid one to behold
Senator
Premium User
Joined
May 6, 2009
Messages
7,019
Solutions
1
Reaction score
1,029
Location
United States
Does anyone know how to make it so it updates the max health of a player?
This function "setCreatureMaxHealth(cid, 1200)" does not work, when you log off, you don't have that health anymore.

I was thinking some kind of database updater that can do it but I don't know how to do that since I'm very noob at life :(

Lua:
function onSay(cid, words, param, channel)
		config = {
			archer_items = {8849, 8892, 2649, 2641, 2461, 2543}
		}
	local container = doPlayerAddItem(cid, 1988, 1)
	if isPlayer(cid) and getPlayerVocation(cid) == 0 and getPlayerStorageValue(cid, 6500) == -1 then
	doPlayerSetVocation(cid, 3)
	doCreatureSay(cid, "You are an archer!", TALKTYPE_ORANGE_1)
	doSendMagicEffect(getPlayerPosition(cid), 14)
		for _, id in ipairs(config.archer_items) do
			doPlayerAddItem(cid, id, 1)
		end
			doAddContainerItem(container, 2120, 1)
			doAddContainerItem(container, 5912, 5)
			doAddContainerItem(container, 5888, 2)
			doAddContainerItem(container, 2789, 1)
			doAddContainerItem(container, 13173, 1)
			setCreatureMaxHealth(cid, 1200)    <!-- THIS DOES NOT WORK -->
	else
	doPlayerSendCancel(cid, 'You already chose your vocation.') 
	doSendMagicEffect(getPlayerPosition(cid), 2)
	end
	return true
end
 
Lua:
local new = 1200
db.executeQuery("UPDATE `players` SET `maxhealth` = " .. new .. " WHERE `name` = " .. db.escapeString(getCreatureName(cid)) .. ";")

-

Here is another way. This is a "permanent function."

functions.lua
Lua:
function setMaxHealth(cid, health)
	return db.executeQuery("UPDATE `players` SET `maxhealth` = " .. health .. " WHERE `name` = " .. db.escapeString(getCreatureName(cid)) .. ";")
end

For example...

Code:
local config = {
	hp = 100
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
	if getPlayerLevel(cid) > 100 then
		[COLOR="red"][B]setMaxHealth(cid, config.hp)[/B][/COLOR]
	else
		doPlayerSendCancel(cid, "Sorry, not possible.")
	end
	
	return true
end
 
Last edited:
If you aren't using indexes, then always add LIMIT 1 in order to boost query performance!

Lua:
function setMaxHealth(cid, health)
	return db.executeQuery("UPDATE players SET maxhealth=" .. health .. " WHERE name=" .. db.escapeString(getCreatureName(cid)) .. " LIMIT 1")
end
A faster, safer and generally better way is to use indexes whenever it's possible (which includes this case):
Lua:
function setMaxHealth(cid, health)
	return db.executeQuery("UPDATE players SET maxhealth=" .. health .. " WHERE id='" .. getPlayerGUID(cid))
end
 
Last edited:
Back
Top