• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

[Talkaction] + [Globalevent] /save command and auto save script. (Simple).

Znote

<?php echo $title; ?>
Staff member
Global Moderator
Premium User
Joined
Feb 14, 2008
Messages
7,037
Solutions
256
Reaction score
2,139
Location
Norway
GitHub
Znote
Hello. TFS save scripts looks good. But I think they are messy. :p Here are two samples I use on my server.

Replace the existing save.lua file:
/data/globalevents/scripts/save.lua
LUA:
function onThink()
	for var = 1,2 do
		if var == 1 then
			doSaveServer()
		end
		if var == 2 then
			doBroadcastMessage("Saved.")
		end
	end
	return true
end
(To choose when this shall auto save enter globalevents.xml and change the time there. (TFS 0.3.6 got 900000 which means 15 minutes).

/save commands for GMs:
/data/talkactions/scripts/save.lua
LUA:
function onSay(cid, words, param, channel)
	if getPlayerGroupId(cid) > 3 then
		for var = 1,2 do
			if var == 1 then
				doBroadcastMessage("Saved.")
			end
			if var == 2 then
				doSaveServer()
			end
		end
	end
	return true
end

And your done. I said it was simple.

Additional stuff (optional):
If you are running a real tibia based map or have really long server saves, you might want it to tell when save starts and when its done:
Replace:
LUA:
	for var = 1,2 do
		if var == 1 then
			doBroadcastMessage("Saved.")
		end
		if var == 2 then
			doSaveServer()
		end
	end
with:
LUA:
	for var = 1,3 do
		if var == 1 then
			doBroadcastMessage("Saving server...") --- this appears before server freeze...
		end
		if var == 2 then
			doSaveServer() --- freeezee....
		end
		if var == 3 then
			doBroadcastMessage("Done.") --- no more lag!
		end
	end
 
Last edited:
Kool! can be done a nicer way:
Code:
local co = 0;
funciton onThink()
	doBroadcastMessage("Saving server...")
	co = coroutine.create(doSaveServer)
	coroutine.resume(co)
	if coroutine.status(co) == "dead" then
		doBroadcastMessage("Done.")
	end
	return true
end
threaded :p threading in lua sucks, tho, since it blocks
 
For example I use a edited Evo map wich is pretty big.. the original save system i got now makes it freeze whenever it saves. Do you think this is better or worse than the one who followes with Tfs 0.3.6?
 
The freeze will be the same. But if you patch my save script with the additional stuff you will broadcast to everybody "saving server..." and they can read that message while its freeze. And they understand OT is not laggy its just a server save. :P
 
Did they change anything in TFS? Old script:
PHP:
function onSay(cid, words, param, channel)
	if getPlayerGroupId(cid) > 3 then
		doBroadcastMessage("Start server save.")
		doSaveServer()
		doBroadcastMessage("Saved.")
	return true
end
does not work fine?
 
Did they change anything in TFS? Old script:
PHP:
function onSay(cid, words, param, channel)
	if getPlayerGroupId(cid) > 3 then
		doBroadcastMessage("Start server save.")
		doSaveServer()
		doBroadcastMessage("Saved.")
	return true
end
does not work fine?

Should work fine. But it might not load the lines in correct order.

Default TFS script that I used before I made my simple scripts:
LUA:
local savingEvent = 0

function onSay(cid, words, param, channel)
	local tmp = tonumber(param)
	if(tmp ~= nil) then
		stopEvent(savingEvent)
		save(tmp * 60 * 1000)
	elseif(param == '') then
		doSaveServer()
	else
		local tid = getPlayerByNameWildcard(param)
		if(not tid or (isPlayerGhost(tid) and getPlayerGhostAccess(tid) > getPlayerGhostAccess(cid))) then
			doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Player " .. param .. " not found.")
		else
			doPlayerSave(tid)
		end
	end

	return true
end

function save(delay)
	doSaveServer()
	if(delay > 0) then
		savingEvent = addEvent(save, delay, delay)
	end
end
 
You can do this too :O?
LUA:
 for var = 1,3 do
	return var == 1 and doBroadcastMessage("Saving server...") or var == 2 and doSaveServer()  or doBroadcastMessage("Done.")
end
 
You can do this too :O?
LUA:
 for var = 1,3 do
	return var == 1 and doBroadcastMessage("Saving server...") or var == 2 and doSaveServer()  or doBroadcastMessage("Done.")
	end
end
No, it will stop the script at first loop
 
However if you dont like 2 if statements inside the loop you can do:
LUA:
for var = 1,2 do
		if var == 1 then
			doSaveServer()
		else
			doBroadcastMessage("Saved.")
		end
	end

In case of the 3 step (start save, SAVE, end save)
LUA:
doBroadcastMessage("Saving...")
for var = 1,2 do
		if var == 1 then
			doSaveServer()
		else
			doBroadcastMessage("Saved.")
		end
	end

It will broadcast "saving." at same time/before the for loop begins.

However its not worth editing main post for. The change is so small and really, who cares if a script runs 0.001 second quicker using one less if statement when a huge server save is coming ahead.
 
Last edited:
How can it execute LUA functions in other order?!

I donno. Just thought that if nothing separates them to make a proper order it will execute all 3 actions at the same time, meaning both the broadcasts will appear at same time as the save and not after it. I haven't actually tested it cause server save is to quick on my OT to notice the difference.

Looks like Fallen<-- (click to view post) had same idea and made a more proper version of it thought. Since it verifies that the save actually is done before it broadcasts it.
 
LUA must work like every other programming language. Execute functions one by one. Can't execute next function if other function is running:
For ex.:
PHP:
print(1)
print(2)
print(3)
always show 1,2,3, not 1,3,2 or 2,3,1
 
Because all 3 actions are "print". What if you do print 1, then save, then print 3?

It will most likely show it as "print 1" "print 3" and "save" in console?

doSaveServer() is a function that calls for a save, but before the save starts the next line in lua script launches and prints as far as i know. see for yourself

If you say "hey! come over here!" to your dog who is 10 meters away from you, then you say "Hey fat dude, come over here!" to your friend who is also 10 meters away from you, and then you say "Hey 2nd dog, come over here!" which is also 10 meters away, then the dogs will most likely come before the fat dude. :p

And then, once the dog arrives to your they will do their actions (which is tiny and small), and the fat dude comes later and then do whatever you wanna do with him. (which is a big loading CPU requiring process).

Remember that lua is only a scripting language. Not a programming language.

Which also might mean that even my for looping script might not work as we want. If thats the case then Fallen is the only script thats legit here as it confirms the save. :p
 
Last edited:
LUA:
return doBroadcastMessage("Saving server..."), doSaveServer(), doBroadcastMessage("Done.")
 
ALL SCRIPTS FROM THIS THREAD DO NOT WORK [TESTED!]
Why?
PHP:
int32_t LuaScriptInterface::luaDoSaveServer(lua_State* L)
{
        //doSaveServer([shallow])
        bool shallow = false;
        if(lua_gettop(L) > 0)
                shallow = popNumber(L);

        Dispatcher::getInstance().addTask(createTask(boost::bind(&Game::saveGameState, &g_game, shallow)));
        return 1;
}
This function does not return anything to LUA... and add task, not execute save, so it's not possible to check when it starts/ends.
ALL scripts show one/both messages AFTER SAVE on my server (11k players in database, 120 online).
Only this script shows message 'Saving..' before save:
PHP:
function onSay(cid, words, param, channel)
	doBroadcastMessage("Saving...")
	addEvent(doSaveServer, 200)
	return true
end
-----------------------
Tests of all functions from this thread (with os.time() and os.clock() at end of each broadcast):
Code:
[COLOR="darkorange"][B]01:56 Saving server...1297817864 - 79146.4
01:56 Done.1297817864 - 79146.4
01:58 Saving server...1297817952 - 79153.3
01:58 Done.1297817952 - 79153.3
01:58 Saving server...1297817987 - 79155.67
01:58 Done.1297817987 - 79155.67
02:00 Saving server...1297818079 - 79162.82
02:00 Done.1297818079 - 79162.82
02:01 Saving server...1297818147 - 79168
02:01 Saving server...1297818182 - 79171.39
02:03 Saving...1297818289 - 79180.08
02:03 Saved.1297818289 - 79180.08
02:03 Saving...1297818301 - 79180.73
02:03 Saved.1297818301 - 79180.73[/B][/COLOR]
:( :( :( :( :( :(
 
I have tested the scripts on a low end OT and the saving itself is working. But looks like messaging aint working. But making an addevent sounds like the soluton. Or just keep the last one.
 
Back
Top