hey, i am back again with some more questions ...
I tried to make a save function for my custom "Hunger Bar" mod, the only problem is that is doesn't work
purpose: when the player leaves with 25% hunger, it should be there when he rejoins
any help is highly appreciated !
[CODE]function HungerSave( ply )
if (file.Exists( "hunger_data/" .. tostring( ply:UniqueID() ) .. "hunger.txt", "DATA" )) then
local Data = util.JSONToTable(file.Read( "hunger_data/" .. ply:UniqueID() .. "hunger.txt", "DATA" ));
for k, v in pairs( Data ) do
ply:SetNWInt("Hunger")
end
end
end
hook.Add("PlayerInitialSpawn", "HungerSave", HungerSave)
function HungerSave( ply )
local filename = "hunger_data/" .. tostring( ply:UniqueID() ) .. "hunger.txt"
local Hungertable = {}
for d, f in pairs( ply:GetNWInt("Hunger") ) do
table.insert( Hungertable, f:GetNWInt("Hunger") )
end
local data = util.TableToJSON( Hungertable )
file.Write( filename, data )
end
hook.Add( "PlayerDisconnected", "HungerSave", HungerSave )[/CODE]
No errors are thrown out.
You're not setting "ply:SetNWInt("Hunger")" to anything at line 5.
@Jacob, i don't know how to work with Pdata, could u show me an example ?
@Brandon, how can i set it to the value it is supposed to load ?
[QUOTE=timmybo5;43933867]@Jacob, i don't know how to work with Pdata, could u show me an example ?
@Brandon, how can i set it to the value it is supposed to load ?[/QUOTE]
PData works similarly to NWInt ( Syntax ), except it doesn't sync with client, but it saves its value over a restart.
@Robotboy, how could i implement it in a save ?
[QUOTE=timmybo5;43934144]@Robotboy, how could i implement it in a save ?[/QUOTE]
What? It saves automatically, but it doesn't send the values to clients.
[editline]16th February 2014[/editline]
ply:SetNWInt( "Hunger", v )
it doesn't work
[CODE]function HungerSave( ply )
if (file.Exists( "hunger_data/" .. tostring( ply:UniqueID() ) .. "hunger.txt", "DATA" )) then
local Data = util.JSONToTable(file.Read( "hunger_data/" .. ply:UniqueID() .. "hunger.txt", "DATA" ));
for k, v in pairs( Data ) do
ply:SetNWInt( "Hunger", v )
end
end
end
hook.Add("PlayerInitialSpawn", "HungerSave", HungerSave)
function HungerSave( ply )
local filename = "hunger_data/" .. tostring( ply:UniqueID() ) .. "hunger.txt"
local Hungertable = {}
for d, f in pairs( ply:GetNWInt("Hunger") ) do
table.insert( Hungertable, f:GetNWInt("Hunger") )
end
local data = util.TableToJSON( Hungertable )
file.Write( filename, data )
end
hook.Add( "PlayerDisconnected", "HungerSave", HungerSave )
[/CODE]
and this error appears
note: the hunger_data file remains empty
[CODE]
[ERROR] addons/server_custom_scripts/lua/autorun/save_hunger_thirst.lua:27: bad argument #1 to 'pairs' (table expected, got number)
1. pairs - [C]:-1
2. v - addons/server_custom_scripts/lua/autorun/save_hunger_thirst.lua:27
3. unknown - lua/includes/modules/hook.lua:82
[/CODE]
Pdata is unrelated to file.Write. It saves to a database, not a .txt file.
@BFG i am not using Pdata, only "NWInt"
[code]for d, f in pairs( ply:GetNWInt("Hunger") ) do[/code]
Your problem lies here. ply:GetNWInt("Hunger") return a number, and pairs expect a table.
mm, how can i let expect a number ?
[QUOTE=timmybo5;43935540]@BFG i am not using Pdata, only "NWInt"[/QUOTE]
Well your best bet would be to use pdata, and also both your functions have the same name, which they shouldnt
[QUOTE=timmybo5;43935670]mm, how can i let expect a number ?[/QUOTE]
You can't. You [b]must[/b] give it a table, that' how how function works.
[QUOTE=Robotboy655;43935730]You can't. You [b]must[/b] give it a table, that' how how function works.[/QUOTE]
How about
[CODE]for i=1, ply:GetNWInt("Hunger") do
//GAYCODE
end[/CODE]
[QUOTE=AirBlack;43936100]How about
[CODE]for i=1, ply:GetNWInt("Hunger") do
//GAYCODE
end[/CODE][/QUOTE]
And what do you expect this to do? It wont magically add all Networked variables to his table.
[QUOTE=timmybo5;43934956]Text[/QUOTE]
You should use [CODE]function HungerSave( ply )
local filename = "hunger_data/" .. tostring( ply:UniqueID() ) .. "hunger.txt"
local Hungertable = {}
table.insert( Hungertable, ply:GetNWInt("Hunger") )// thx Bo98
local data = util.TableToJSON( Hungertable )
file.Write( filename, data )
end
hook.Add( "PlayerDisconnected", "HungerSave", HungerSave )[/CODE]
[editline]17th February 2014[/editline]
[QUOTE=Robotboy655;43936119]And what do you expect this to do? It wont magically add all Networked variables to his table.[/QUOTE]
Sry, i haven't understand problem.
[QUOTE=AirBlack;43936135][CODE]function HungerSave( ply )
local filename = "hunger_data/" .. tostring( ply:UniqueID() ) .. "hunger.txt"
local Hungertable = {}
table.insert( Hungertable, f:GetNWInt("Hunger") )
local data = util.TableToJSON( Hungertable )
file.Write( filename, data )
end
hook.Add( "PlayerDisconnected", "HungerSave", HungerSave )[/CODE][/QUOTE]
That f: should be ply:
OP, you can incoorporate this code by changing names and setting a max for SetMoney(SetHunger).
[URL="http://facepunch.com/showthread.php?t=1315437&p=42524696&viewfull=1#post42524696"]http://facepunch.com/showthread.php?t=1315437&p=42524696&viewfull=1#post42524696[/URL]
Commented version:
[URL="http://facepunch.com/showthread.php?t=1315437&p=42529130&viewfull=1#post42529130"]http://facepunch.com/showthread.php?t=1315437&p=42529130&viewfull=1#post42529130[/URL]
[code]local savedNWVars = {
"Hunger",
"Whatever"
}
function LoadNWVars( ply )
if ( file.Exists( "nwdata/" .. tostring( ply:UniqueID() ), "DATA" )) then
local Data = util.JSONToTable( file.Read( "nwdata/" .. ply:UniqueID(), "DATA" ));
for k, v in pairs( Data ) do
ply:SetNetworkedVar( k, v )
end
end
end
hook.Add( "PlayerInitialSpawn", "LoadNWVars", LoadNWVars )
function SaveNWVars( ply )
local NWData = {}
for k, v in pairs( savedNWVars ) do
NWData[ v ] = ply:GetNetworkedVar( v )
end
local data = util.TableToJSON( NWData )
file.Write( "nwdata/" .. tostring( ply:UniqueID() ), data )
end
hook.Add( "PlayerDisconnected", "SaveNWVars", SaveNWVars )
hook.Add( "ShutDown", "HungerSave", function()
for id, ply in pairs( player.GetAll() ) do SaveNWVars( ply ) end
end )
[/code]
[editline]16th February 2014[/editline]
Saves all NWVars you put into the top table, untested.
Testing it atm
[editline]16th February 2014[/editline]
It doesn't work, not even a file appears in the nwdata folder
Bump
Solved it myself, thanks for the help anyways !
Sorry, you need to Log In to post a reply to this thread.