Jump to content

SavePlayerData


CodX

Recommended Posts

Posted

Hello !
 

function savePlayerData(thePlayer)
	local account = getPlayerAccount(thePlayer);
	if account then
		for _,v in ipairs(playerDataTable) do
			setAccountData(account, v[1], getElementData(thePlayer, v[1]));
		end
		local x,y,z = getElementPosition(thePlayer);
		setAccountData(account, "last_x", x);
		setAccountData(account, "last_y", y);
		setAccountData(account, "last_z", z);
	end
end

I have some problems with this script. He always get player name as "nil" and i don't know why can't.

Warning Text from debugger: Bad Argument @'getPlayerAccount' [Expected element at argument 1, got nil]

Posted
1 minute ago, NeXuS™ said:

How do you call the savePlayerData function?

setTimer(function()
	savePlayerData(source)
end, 1000, 0);

 

Posted
Just now, NeXuS™ said:

addEventHandler("onPlayerQuit", root, function()
	savePlayerData(source)
end)

Try it with this.

I've tried this but sometimes don't save playerData when the player has disconnected.

Posted
Just now, NeXuS™ said:

There is no way it isn't saved with this method. It is called every single time a player is disconnected.

Sometiems didn't save my data, that is the reason why i've used a timer.

Posted (edited)

If you want to create an unoptimized script, here ya go.

setTimer(function()
	for i, k in pairs(getElementsByType("player")) do
		savePlayerData(k)
	end
end, 1000, 0)

 

Edited by NeXuS™
  • Thanks 1
Posted
3 minutes ago, NeXuS™ said:

If you want to create an unoptimized script, here ya go.


setTimer(function()
	for i, k in pairs(getElementsByType("player")) do
		savePlayerData(k)
	end
end, 1000, 0)

 

Work !
Thank you for help !

Posted
8 hours ago, NeXuS™ said:

There is no way it isn't saved with this method. It is called every single time a player is disconnected.

In fact, there are at least two ways. One way is that the server crashes; nothing you can do there.. or that the resource gets stopped and a player leaves within the time the resource is not running; this can be solved using onResourceStop.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...