• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Save house (this house can be YOURS or not) [HELP]

LucasFerraz

Systems Analyst
Joined
Jun 10, 2010
Messages
2,857
Reaction score
96
Location
Brazil
Hello guys :D,


This script below is a feature(OnMove) posted here in OTLand and It's checking if player is moving something inside of a house.
If inside a house then script must save PLAYER(doPlayerSave(cid)) and HOUSE(doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))} to save player's house)).
The problem is: this player can be only an guess.
How can I save this house if the player who's moving is NOT the owner?
LUA:
function onMoveItem(cid, item, formPosition, toPosition, fromItem, toItem, fromGround, toGround, status)
	if (getTileInfo(fromPosition).house) then
             doPlayerSave(cid)
             doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))})
	end
	return true
end

I must fix it to update this: GlobalEvent Server Save [Optimized, less lag] v1.8
Your credits will be posted. I just tried a LOT and couldn't fix it myself.
 
Nice idea, but every moving something inside of a house saving house. Do you think it will be Optimized? for example someone throw 20 items to house within 20~ seconds so script save house full of items 20 times?

Code:
function onMoveItem(cid, item, formPosition, toPosition, fromItem, toItem, fromGround, toGround, status)
	if (getTileInfo(fromPosition).house) then
             doPlayerSave(cid)
             [COLOR="#FF0000"]doSaveHouse({getHouseFromPos(fromPosition)}) -- maybe it will work[/COLOR]
	end
	return true
end
 
Last edited:
LUA:
function onMoveItem(cid, item, fromPosition, toPosition, fromItem, toItem, fromGround, toGround, status)
	local tileInfoFrom = getHouseFromPos(fromPosition)
	local tileInfoTo = getHouseFromPos(toPosition)
	if(tileInfoFrom or tileInfoTo) then
		doPlayerSave(cid)
	end
	if(tileInfoFrom) then
		doSaveHouse(tileInfoFrom)
	end
	if(tileInfoTo and (type(tileInfoFrom) == "boolean" or tileInfoFrom ~= tileInfoTo)) then
		doSaveHouse(tileInfoTo)
	end
	return true
end
It will save player and house in which he moves item (or 2 houses if he throw item from house to house!).

Function getHouseFromPos(pos) return false if there is no house or number of house if there is house [on 0.3.6, you must check if it's same on 0.4].
 
It will save if player move ground to ground inside of a house right?
What about:
He moves an item from inventory/backpack to ground(house)?
He moves an item from ground(house) to inventory/backpack?
He moves an item from ground(outside) to inside?
He moves an item from ground(inside) to outside?

I could make what you did (not so optimized like yours :D). Thank you anyway sz

Maybe will be better if after moving the "last item" save player and house using add event to prevent lags/kicks?

- - - Updated - - -

I'll try it, thanks
 
if(tileInfoFrom or tileInfoTo) then
If he moves item inside house or from backpack to house or from house to backpack it should save that house and player.

EDIT:
with addEvent it will lag anyway, events added in 'add event' lag server same as function executed in script. TFS use one CPU core [to keep all synchronized] and execute one script after another.

TEST talkaction text:
LUA:
!lua addEvent(function(toTime) while(os.time() < toTime) do end end, 1, os.time()+4)
It freezes server for 4 seconds, same as:
LUA:
!lua function testFreez(toTime) while(os.time() < toTime) do end end testFreez(os.time()+4)
(test with http://otland.net/f81/debug-lua-scripts-talkaction-112027/ )
 
Last edited:
Omg perfect. So i just have to think about addevent(save). Thank you for your help!

- - - Updated - - -

if(tileInfoFrom or tileInfoTo) then
If he moves item inside house or from backpack to house or from house to backpack it should save that house and player.

EDIT:
with addEvent it will lag anyway, events added in 'add event' lag server same as function executed in script. TFS use one CPU core [to keep all synchronized] and execute one script after another.

TEST talkaction text:
LUA:
!lua addEvent(function(toTime) while(os.time() < toTime) do end end, 1, os.time()+4)
It freezes server for 4 seconds, same as:
LUA:
!lua function testFreez(toTime) while(os.time() < toTime) do end end testFreez(os.time()+4)
(test with http://otland.net/f81/debug-lua-scripts-talkaction-112027/ )

What if it saves only after the LAST move inside of a house?
 
What about it:
- OnMove before all, cancel addevent
- OnMove after all, addevent(will save in 10 seconds)

So it will save only the last move, dont it?

- - - Updated - - -

Error when moving an item inside or outside of a house
121212.jpg

- - - Updated - - -

Solved.
Changed fromPosition to formPosition

- - - Updated - - -

Your Part

LUA:
	local tileInfoFrom = getHouseFromPos(formPosition)
	local tileInfoTo = getHouseFromPos(toPosition)
	if (tileInfoFrom or tileInfoTo) then
		doPlayerSave(cid)
		doPlayerSendCancel(cid, "Save 1.")
	end
	if (tileInfoFrom) then
		doSaveHouse(tileInfoFrom)
		doPlayerSendCancel(cid, "Save 2.")
	end
	if (tileInfoTo and (type(tileInfoFrom) == "boolean" or tileInfoFrom ~= tileInfoTo)) then
		doSaveHouse(tileInfoTo)
		doPlayerSendCancel(cid, "Save 3.")
	end

My full script
LUA:
local storage = 61894
local exhause_time = 500 -- in milli seconds

local function resetStorage(cid)
    if isPlayer(cid) then
        setPlayerStorageValue(cid, storage, 0)
    end
end 
 
local function doExhaust(cid)
	-- exhaust divided by 1000 can be 0 (500 / 1000 = 0), but in onMoveItem(..) we check if time is >= os.time()
	setPlayerStorageValue(cid,storage,os.time()+(exhause_time / 1000))
	-- so if 'event' does not execute, it won't be a problem (just block for 1 second [not 0.5 sec] IF you relog)
	addEvent(resetStorage, exhause_time, cid)
end
 
function onMoveItem(cid, item, formPosition, toPosition, fromItem, toItem, fromGround, toGround, status)
	-- and here we check time, not 1/0 value
	if getPlayerStorageValue(cid,storage) >= os.time() then
		doPlayerSendCancel(cid, "You can't move items that fast, please wait a few seconds.")
		return false
	end
	doExhaust(cid)
	-- If inside of a house, save house and player
	--if (getTileInfo(formPosition).house) or (getTileInfo(fromGround).house) or (getTileInfo(toGround).house) or (getTileInfo(fromItem).house) or (getTileInfo(toItem).house) then
		--if status.inInv == 0 or status.inInv == 1 or status.inInv == 3 or status.inInvBag == 0 or status.inInvBag == 1 or status.inInvBag == 3 then
			--doPlayerSave(cid)
		--end
		--doSaveHouse({getHouseFromPos(getCreaturePosition(cid))})
	--end

	local tileInfoFrom = getHouseFromPos(formPosition)
	local tileInfoTo = getHouseFromPos(toPosition)
	if (tileInfoFrom or tileInfoTo) then
		doPlayerSave(cid)
		doPlayerSendCancel(cid, "Save 1.")
	end
	if (tileInfoFrom) then
		doSaveHouse(tileInfoFrom)
		doPlayerSendCancel(cid, "Save 2.")
	end
	if (tileInfoTo and (type(tileInfoFrom) == "boolean" or tileInfoFrom ~= tileInfoTo)) then
		doSaveHouse(tileInfoTo)
		doPlayerSendCancel(cid, "Save 3.")
	end
	return true
end

No matter which move I do inside of the house. It just do 'Save 2'.
It just 'Save 3' from Outside to Inside.
'Save 1' is not happening.
 
Last edited:
Back
Top