PData not saving on restart.

So, I’m trying to create a credit system for my server, I’m trying to save some values, the one below is setting and saving max health data, it doesn’t seem to be saving on restarts/crashes.


util.AddNetworkString( "CreditMaxHealth" )

hook.Add( "PlayerInitialSpawn", "SpawnCheck", function(ply)

	if(ply:GetPData( "MHealth" ) == nil) then
		ply:SetNWInt( "MHealth", 1 )
	else
		ply:SetNWInt( "MHealth", ply:GetPData( "MHealth" ) )
	end

end)

hook.Add( "PlayerSpawn", "CheckSpawn", function(ply)

	if ply:GetNWInt( "MHealth" ) == 2 then
		timer.Simple( 1, function()
			ply:SetMaxHealth(150)
			ply:SetHealth(150)
		end)
	end

end)

hook.Add( "PlayerDisconnected", "DisonnectCheck", function(ply)

	ply:SetPData( "MHealth", ply:GetNWInt( "MHealth" ) )

end)

hook.Add( "Shutdown", "CrashCheck", function()

	for k, v in pairs( player.GetAll() ) do
		v:SetPData( "MHealth", v:GetNWInt( "MHealth" ) )
	end

end)

net.Receive( "CreditMaxHealth", function( len, ply )

	ply:SetNWInt( "MHealth", ply:GetNWInt( "MHealth" ) + 1 )

	ply:SetMaxHealth(150)
	ply:SetHealth(150)

end)

Any ideas?

Could be because it’s “ShutDown”, not “Shutdown”.

Nope, not because of that.

PData automatically saves into the sqlite database sv.db

You can remove your check for nil inside PlayerInitialSpawn by using default parameters:

ply:GetPData(‘key’, 1)
1 = default value if nil

Your CreditMaxHealth is not updating PData.

If this is on a server, you don’t need to call GetNW when you can continue using GetPData. Shouldn’t make too much of a difference anyway.

Managed to fix it after a while, just kept playing around hoping for the best.
Thank you though guys!