• 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!

GlobalEvent Server Save [Optimized, less lag] v1.8

everything is made on the sources and executed on Lua, the thing is in the sources you directly save the player, but if you do it from Lua, you have to go thru more code to do the same, which makes it "worse"
 
Doing it with onThink or addEvent onLogin should be totally fine and lagfree. Only saving one player at a time should be the best way to do it.

The lua way of doing it shouldn't take much long, but as save occures very often and you don't really need to change the procedure it would be good if there was a source version.
 
Ok thanks guys :D I hope you login tomorrow Summ ^^ im still excited ;D

- - - Updated - - -

I added something which can also may prevent lagg, because of multiple login:

Lua:
local random_start = math.random(300, 400)

function saveRepeat(cid)
        if isPlayer(cid) then
		doSavePlayerAndHouse(cid)
		save = addEvent(saveRepeat, random_start*1000, cid)
        end
return true
end
 
function onLogin(cid)
	save = addEvent(saveRepeat, random_start*1000, cid)
	return true
end
 
function onLogout(cid)
	doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))})
	stopEvent(save)
	return true
end
 
I've thinked over it and I am not sure if I should use this. Is it 100% working? and where can I get the functions like: doSavePlayerAndHouse(cid)
 
Ok thanks guys :D I hope you login tomorrow Summ ^^ im still excited ;D

- - - Updated - - -

I added something which can also may prevent lagg, because of multiple login:

Lua:
local config = { --Times are in seconds
    saveInterval = 5 * 60,
    minSaveInterval = 4 * 60,
    maxSaveInterval = 6 * 60
}

function saveRepeat(cid)
        if isPlayer(cid) then
        doSavePlayerAndHouse(cid)
        save = addEvent(saveRepeat, config.saveInterval*1000, cid)
        end
return true
end
 
function onLogin(cid)
    save = addEvent(saveRepeat, math.random(config.minSaveInterval, config.maxSaveInterval) * 1000, cid)
    return true
end
 
function onLogout(cid)
    doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))})
    stopEvent(save)
    return true
end
having the math.random value in a variable will lead to the same problem since random_start is just calculated when the script is loaded, its better if you have the random in the addEvent call like I changed on my quoted text
I also added a config so a minimum and a maximum can be specified, you also only need to change the initial wait time to avoid possible lag when multiple players login at same time, after that the interval time can be the same

I've thinked over it and I am not sure if I should use this. Is it 100% working? and where can I get the functions like: doSavePlayerAndHouse(cid)
Lua:
local function doSavePlayerAndHouse(cid)
    doPlayerSave(cid)
    doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))})
    return true
end
 
Can you fix it the best way you see?
Lua:
local config = { --Times are in seconds
    saveInterval = 5 * 60,
    minSaveInterval = 4 * 60,
    maxSaveInterval = 6 * 60
}
 
function saveRepeat(cid)
        if isPlayer(cid) then
        doSavePlayerAndHouse(cid)
        save = addEvent(saveRepeat, config.saveInterval*1000, cid)
        end
return true
end
 
function onLogin(cid)
    save = addEvent(saveRepeat, math.random(config.minSaveInterval, config.maxSaveInterval) * 1000, cid)
    return true
end
 
function onLogout(cid)
    doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))})
    stopEvent(save)
    return true
end
 
Can you fix it the best way you see?
Lua:
local config = { --Times are in seconds
    saveInterval = 5 * 60,
    minSaveInterval = 4 * 60,
    maxSaveInterval = 6 * 60
}
 
function saveRepeat(cid)
        if isPlayer(cid) then
        doSavePlayerAndHouse(cid)
        save = addEvent(saveRepeat, config.saveInterval*1000, cid)
        end
return true
end
 
function onLogin(cid)
    save = addEvent(saveRepeat, math.random(config.minSaveInterval, config.maxSaveInterval) * 1000, cid)
    return true
end
 
function onLogout(cid)
    doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))})
    stopEvent(save)
    return true
end
that seems to be fine, just change the doSavePlayerAndHouse into the actual functions(doSavePlayer and doSaveHouse) or add the function to the code(I wrote it above)
 
Isn't it better?
Lua:
local config = { --Times are in seconds
    saveInterval = 5 * 60,
    minSaveInterval = 4 * 60,
    maxSaveInterval = 6 * 60
}
 
function saveRepeat(cid)
        if isPlayer(cid) then
        doSavePlayerAndHouse(cid)
        save = addEvent(saveRepeat, math.random(config.minSaveInterval, config.maxSaveInterval) * 1000, cid)
        end
return true
end
 
function onLogin(cid)
    save = addEvent(saveRepeat, math.random(config.minSaveInterval, config.maxSaveInterval) * 1000, cid)
    return true
end
 
function onLogout(cid)
    doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))})
    stopEvent(save)
    return true
end
 
no, in the saveRepeat you should use the saveInterval one instead of making another random

we only make the random in the login because of this situation:
(without random) - 3 players enter at the same time, those 3 players will save every 5 minutes at the same time, that way:
Player 1 - Saves @5, saves @ 10, saves @15, ...
Player 2 - Saves @5, saves @ 10, saves @15, ...
Player 3 - Saves @5, saves @ 10, saves @15, ...
(with random only in start) - 3 players enter at the same time, each player will have a random time for the first save between 4 and 6 minutes, and after that every 5 minutes, so they never save at the same time, that way:
Player 1 - Saves @ 4, saves @ 9, saves @14, ..
Player 2 - Saves @ 5, saves @ 10, saves @15, ...
Player 3 - Saves @ 4.3, saves @ 9.3,saves @14.3, ...

its true that it also works with both being random, but there's no need to waste more CPU time making another random if each save is already unique
 
I updated your code in the first post(hope you dont mind)

I also just noticed a problem, you were using a variable(save) to store the ID of the event so you could later stop it, since you didnt created it in the script it would be 0 on the logout script, so to fix that I made it so that the event ID is stored in a playerStorage
 
What is wrong with it?
Lua:
local config = {
    saveInterval = 6 * 60,
    minSaveInterval = 4 * 60,
    maxSaveInterval = 8 * 60,
    storage = 16046
}
 
function saveRepeat(cid)
	if isPlayer(cid) then
		doSavePlayerAndHouse(cid)
		setPlayerStorageValue(cid, config.storage, addEvent(saveRepeat, config.saveInterval*1000, cid))
	end
return true
end
 
function onLogin(cid)
	if (getPlayerAccount(getPlayerByName(name)) ~= 1) or (getPlayerAccount(getPlayerByName(name)) ~= 2) then
		setPlayerStorageValue(cid, config.storage, addEvent(saveRepeat, math.random(config.minSaveInterval, config.maxSaveInterval) * 1000, cid))
	end
return true
end
 
function onLogout(cid)
    doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))})
    stopEvent(getPlayerStorageValue(cid, config.storage))
    return true
end

- - - Updated - - -

It's not getting account id
 
Saving is a SQL intensive task. You want less lag when saving?
Use MariaDB!

Want even less lag?
Compile MariaDB yourself and use "-march=native" compiler flag.

Want even less lag?
Use InnoDB tables!

Want even less lag?
Overhaul the Schema!

Want even less lag?
Use HugePages on MariaDB!


When I had done all that, my server could save with 120+ players online in less than a 1.6~ seconds.

Note that all of the above could be combined with what you're doing here. But it's the sort of thing that should have been done first when trying to optimize. I.e in my opinion you're trying to optimize in the wrong place here. Now if you had done all that and it wasn't enough, I could see trying to make a 'staggered save' system in Lua, but still, it's kind of silly.
 
Last edited:
What if owner of a house for example get banned, he using second character and his house as well. House won't save because owner is offline all the time?
 
Last edited:
Back
Top