• GMod - Wont save file! (file.Write)
    7 replies, posted
Hi again.. Well, now I have another problem with my gamemode, it just wont save the file I want it to, its a 'money system'. Its the first time I use the file functions, but heres the code, I hope you can help me :-I. I want it to safe the file at [b]GDom\content\data\[/b], but it doesnt save it anywhere. Code: [code] ---------------------------- -- Money System ---------------------------- function loadMoney( ply ) if(file.Exists( ply:SteamID() .. ".txt" )) then ply:PrintMessage(HUD_PRINTTALK, "Your money was loaded!\n") ply:PrintMessage(HUD_PRINTTALK, "You currently have " .. getMoney( ply ) .. "$") else ply:PrintMessage(HUD_PRINTTALK, "The system didnt find your money file..\nCreating..\n") file.Write( "GDom\content\data\" .. ply:SteamID() .. ".txt", 15 ) ply:PrintMessage(HUD_PRINTTALK, "Created! You do as default have 15$") end end function saveMoney( ply, ammount ) if(file.Exists( ply:SteamID() .. ".txt" )) then file.Write( "GDom\content\data\" .. ply:SteamID() .. ".txt", ammount ) else ply:PrintMessage(HUD_PRINTTALK, "The system didnt find your money file..\nCreating..\n") file.Write( "GDom\content\data\" .. ply:SteamID() .. ".txt", 15 ) ply:PrintMessage(HUD_PRINTTALK, "Created! You do as default have 15$") end ply:PrintMessage(HUD_PRINTTALK, "Money saved!") end function getMoney( ply ) if(file.Exists( ply:SteamID() .. ".txt" )) then return tonumber(file.Read( ply:SteamID() .. ".txt")) else ply:PrintMessage(HUD_PRINTTALK, "The system didnt find your money file..\nCreating..\n") file.Write( "GDom\content\data\" .. ply:SteamID() .. ".txt", 15 ) ply:PrintMessage(HUD_PRINTTALK, "Created! You do as default have 15$") return 15 end end [/code]
You should use UniqueID, that way the file is in proper format.
Whats uniqueID? I never got it, aint it like, when the player joins, the player gets the ID 1, then the second player that joins gets the ID 2 and so on? Or is it totally different?
I think if I remember right its hash of the SteamID?
This number is based off the IP for LAN servers, and the steamid for internet servers. Source: [b][url]http://wiki.garrysmod.com/?title=Player.UniqueID[/url][/b] [editline]05:40PM[/editline] So, im gonna go do that and see what happens. [editline]05:50PM[/editline] Still, no file :-(. [editline]05:52PM[/editline] The new code is: [code] ---------------------------- -- Money System ---------------------------- function loadMoney( ply ) if(file.Exists( "GDom\content\data\\" .. ply:UniqueID() .. ".txt" )) then ply:PrintMessage(HUD_PRINTTALK, "Your money was loaded!\n") ply:PrintMessage(HUD_PRINTTALK, "You currently have " .. getMoney( ply ) .. "$") else ply:PrintMessage(HUD_PRINTTALK, "The system didnt find your money file..\nCreating..\n") file.Write( "GDom\content\data\\" .. ply:UniqueID() .. ".txt", 15 ) ply:PrintMessage(HUD_PRINTTALK, "Created! You do as default have 15$") end end function saveMoney( ply, ammount ) if(file.Exists( "GDom\content\data\\" .. ply:UniqueID() .. ".txt" )) then file.Write( "GDom\content\data\\" .. ply:UniqueID() .. ".txt", ammount ) else ply:PrintMessage(HUD_PRINTTALK, "The system didnt find your money file..\nCreating..\n") file.Write( "GDom\content\data\\" .. ply:UniqueID() .. ".txt", 15 ) ply:PrintMessage(HUD_PRINTTALK, "Created! You do as default have 15$") end ply:PrintMessage(HUD_PRINTTALK, "Money saved!") end function getMoney( ply ) if(file.Exists( "GDom\content\data\\" .. ply:UniqueID() .. ".txt" )) then return tonumber(file.Read( ply:UniqueID() .. ".txt")) else ply:PrintMessage(HUD_PRINTTALK, "The system didnt find your money file..\nCreating..\n") file.Write( "GDom\content\data\\" .. ply:UniqueID() .. ".txt", 15 ) ply:PrintMessage(HUD_PRINTTALK, "Created! You do as default have 15$") return 15 end end [/code]
Try forward slashes.
Lol, thanks guys :-) Originally I posted a code without the file.Exists being touched, but when I quickly edited that and posted the new one instead, and then restarting the server, it actually worked.
The steam id has COLONS ':' These are not usable for files in any operating system. To use the steam id, which I find better, use string.gsub( ply:SteamID(), ":", "_" )
Sorry, you need to Log In to post a reply to this thread.