HomeHome  CalendarCalendar  FAQFAQ  SearchSearch  MemberlistMemberlist  UsergroupsUsergroups  RegisterRegister  Log inLog in  

Share | 
 

 Need help with saving stuffs...

View previous topic View next topic Go down 
AuthorMessage
timpo204



Posts : 7
Reputation : 0
Join date : 2010-10-17

PostSubject: Need help with saving stuffs...   Sun Oct 17, 2010 1:26 am

ok so i have tried this code and mumled and shit in 6 hours now and it will not work!!!!

function meta:SaveMoneyTXT()
file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetMoneyString())


i have a folder called Money and i have a txt in it with the name Money.txt but it will not change or anything Sad

it should save when i dissconnect

function fPlayerDisconnect( ply )
print("Player Disconnect: Money saved to SQLLite and TXT")
ply:SaveMoney()
ply:SaveMoneyTXT()
end


i have only tested it on singleplayer so i dont know if it is that or something but please help me!

i hope i have detailed everything....
Back to top Go down
View user profile
Zemog
TG Colonel
TG Colonel
avatar

Posts : 2210
Reputation : 33
Join date : 2009-05-27
Age : 23

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 10:15 am

I'll ask our evil overloard and coder. Hang on.

EDIT: Eh, you'll have to wait for him to be online.
Back to top Go down
View user profile http://cdn.dipity.com/uploads/events/d219f59aaf68268b76b8b489c11
timpo204



Posts : 7
Reputation : 0
Join date : 2010-10-17

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 10:28 am

okay i will be waiting ;p
Back to top Go down
View user profile
[TG] Chief (alex) -Admin-
TG Leader
TG Leader
avatar

Posts : 1525
Reputation : 59
Join date : 2009-05-17
Age : 25
Location : I am from norway, north norway!

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 12:33 pm

Muppet! You cant write a new file over another one!

Use this function instead when you want to update/create a file:


Code:

function meta:SaveMoneyTXT()
    local location = tostring( gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt" )
    if file.Exists(location) then
        file.Delete(location)
        file.Write(location, self:GetMoneyString())
    else
        file.Write(location, self:GetMoneyString())
    end
end


_________________
...require additional pylons.
Back to top Go down
View user profile http://www.thegoatriders.com
timpo204



Posts : 7
Reputation : 0
Join date : 2010-10-17

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 12:44 pm

still cant get it working here is the full code


local meta = FindMetaTable("Player") --Get the meta table of player

function meta:AddMoney(amount)
local current_cash = self:GetMoney()
self:SetMoney( current_cash + amount )
end

function meta:SetMoney(amount)
self:SetNetworkedInt( "Money", amount )
self:SaveMoney()
end

function meta:SaveMoney()
local cash = self:GetMoney()
self:SetPData("money", cash)
end

function meta:SaveMoneyTXT()
local location = tostring( gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt" )
if file.Exists(location) then
file.Delete(location)
file.Write(location, self:GetMoney())
else
file.Write(location, self:GetMoney())
end
end

function meta:TakeMoney(amount)
--Add money function here
self:AddMoney(-amount)
end

function meta:GetMoney()
return self:GetNetworkedInt( "Money" )
end




and here is the init.lua


AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'sh_player.lua' )
AddCSLuaFile( "sh_player.lua" )

include( 'shared.lua' )



MONEY_STARTAMOUNT = 1000 --Can be changed to your starting amount

function FirstSpawn( ply )
local cash = ply:GetPData("money") --Get the saved money amount

if cash == nil then --If it doesn't exist supply the player with the starting money amount
ply:SetPData("money", MONEY_STARTAMOUNT) --Save it
ply:SetMoney( MONEY_STARTAMOUNT ) --Set it to the networked ints that can be called from the client too
else
ply:SetMoney( cash ) --If not, set the networked ints to what we last saved
end

end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )

function PrintCash( pl )
pl:ChatPrint("Your cash is: " .. pl:GetMoney())
end

function fPlayerDisconnect( ply )
print("Player Disconnect: Money saved to SQLLite and TXT")
ply:SaveMoney()
ply:SaveMoneyTXT()
end

concommand.Add("cash_get",PrintCash)

function ManualSaver( ply )
ply:SaveMoneyTXT()
end

concommand.Add("cash_save",ManualSaver)



and here is my setup of txt's C:\Program Files (x86)\Steam\steamapps\timpalajnen\garrysmod\garrysmod\gamemodes\EvB in here is a txt

C:\Program Files (x86)\Steam\steamapps\timpalajnen\garrysmod\garrysmod\gamemodes\EvB\Money and in here is a txt
Back to top Go down
View user profile
[TG] Chief (alex) -Admin-
TG Leader
TG Leader
avatar

Posts : 1525
Reputation : 59
Join date : 2009-05-17
Age : 25
Location : I am from norway, north norway!

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 12:59 pm

File.write ONLY creates files under garrysmod/data/"gamemode name".

_________________
...require additional pylons.
Back to top Go down
View user profile http://www.thegoatriders.com
timpo204



Posts : 7
Reputation : 0
Join date : 2010-10-17

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 1:04 pm

THANKS!!!!!! it works now!
Back to top Go down
View user profile
[TG] Chief (alex) -Admin-
TG Leader
TG Leader
avatar

Posts : 1525
Reputation : 59
Join date : 2009-05-17
Age : 25
Location : I am from norway, north norway!

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 1:37 pm

Nu prublemo!

EDIT:
Notice the post time. Im god.

_________________
...require additional pylons.
Back to top Go down
View user profile http://www.thegoatriders.com
timpo204



Posts : 7
Reputation : 0
Join date : 2010-10-17

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 1:39 pm

one last thing how do i like load it ?


would be great if you can code it for me as a tutorial and explain what everything does

Back to top Go down
View user profile
[TG] Chief (alex) -Admin-
TG Leader
TG Leader
avatar

Posts : 1525
Reputation : 59
Join date : 2009-05-17
Age : 25
Location : I am from norway, north norway!

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 1:55 pm

Code:

function meta:LoadMoneyTXT()
    local location = tostring( gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt" )
    if file.Exists(location) then
        return file.Read(location )
    else
        return false
    end
end

this function will return what is written in the TXT file.

So if you would go about doing this:

print(ply:LoadMoneyTXT())

it would print the content of the file.

_________________
...require additional pylons.
Back to top Go down
View user profile http://www.thegoatriders.com
[TGR] Dexter
TG Captain
TG Captain


Posts : 484
Reputation : 11
Join date : 2010-01-01

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 1:57 pm

:ninja:
Bladdy hell, if i was here sooner i woulda showed you my supercoding expertise.
Back to top Go down
View user profile http://www.dextah.com
timpo204



Posts : 7
Reputation : 0
Join date : 2010-10-17

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 2:10 pm

init.lua look like this now and doesent working:

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'sh_player.lua' )
AddCSLuaFile( "sh_player.lua" )

include( 'shared.lua' )



MONEY_STARTAMOUNT = 1000 --Can be changed to your starting amount


function PrintCash( pl )
pl:ChatPrint("Your cash is: " .. pl:GetMoney())
end

function fPlayerDisconnect( ply )
print("Player Disconnect: Money saved to SQLLite and TXT")
ply:SaveMoney()
ply:SaveMoneyTXT()
end

concommand.Add("cash_get",PrintCash)

function ManualSaver( ply )
ply:SaveMoneyTXT()
end

concommand.Add("cash_save",ManualSaver)

function MoneyPerKill(victim, killer, weapon)
killer:SetNWInt("Money", killer:GetNWInt("Money") + 25)
end
hook.Add("OnNPCKilled","MoneyPerKill",MoneyPerKill)


function meta:LoadMoneyTXT()
local location = tostring( gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt" )
if file.Exists(location) then
return file.Read(location )
else
ply:SetMoney( MONEY_STARTAMOUNT )
end
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )



and sh_player

local meta = FindMetaTable("Player") --Get the meta table of player

function meta:AddMoney(amount)
local current_cash = self:GetMoney()
self:SetMoney( current_cash + amount )
end

function meta:SetMoney(amount)
self:SetNetworkedInt( "Money", amount )
self:SaveMoney()
end

function meta:SaveMoney()
local cash = self:GetMoney()
self:SetPData("money", cash)
end

function meta:SaveMoneyTXT()
local location = tostring( gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt" )
if file.Exists(location) then
file.Delete(location)
file.Write(location, self:GetMoney())
else
file.Write(location, self:GetMoney())
end
end

function meta:TakeMoney(amount)
--Add money function here
self:AddMoney(-amount)
end

function meta:GetMoney()
return self:GetNetworkedInt( "Money" )
end




still not working please fix it to me Smile PS: I love goats xD


the saving is working but not loading i want it to load my cash :p
Back to top Go down
View user profile
timpo204



Posts : 7
Reputation : 0
Join date : 2010-10-17

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 4:30 pm

No one want to help me with the last thing ? Sad
Back to top Go down
View user profile
Spypmeister
TG Lieutenant - Lan host
TG Lieutenant - Lan host
avatar

Posts : 232
Reputation : 3
Join date : 2009-06-06
Age : 25
Location : Norway... NORTH Norway

PostSubject: Re: Need help with saving stuffs...   Sun Oct 17, 2010 6:39 pm

Our coders are troubled with this one, good sir.
They will be finished in:
Time remaining: unknown.
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: Need help with saving stuffs...   

Back to top Go down
 
Need help with saving stuffs...
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» Mozilla Firefox users...Mai-HiME/Otome stuffs for you!
» Daylight Saving Time
» 'Smallville's' Clark Kent Prepares To Suit Up
» Skate Parks Location (For bashing? Jumping? Doing crazy stuffs" :D )
» Favourite all time characters vs all time worst characters

Permissions in this forum:You cannot reply to topics in this forum
 :: Coding-
Jump to: