GMod - Wont save file! (file.Write)

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 **GDom\content\data**, but it doesnt save it anywhere.

Code:



----------------------------
-- Money System
----------------------------
 
function loadMoney( ply )
	if(file.Exists( ply:SteamID() .. ".txt" )) then
		ply:PrintMessage(HUD_PRINTTALK, "Your money was loaded!
")
		ply:PrintMessage(HUD_PRINTTALK, "You currently have " .. getMoney( ply ) .. "$")
	else
		ply:PrintMessage(HUD_PRINTTALK, "The system didnt find your money file..
Creating..
")
		
		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..
Creating..
")
		
		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..
Creating..
")
		
		file.Write( "GDom\content\data\" .. ply:SteamID() .. ".txt", 15 )
		ply:PrintMessage(HUD_PRINTTALK, "Created! You do as default have 15$")	
		return 15
	end
end


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: http://wiki.garrysmod.com/?title=Player.UniqueID

[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:



----------------------------
-- Money System
----------------------------
 
function loadMoney( ply )
	if(file.Exists( "GDom\content\data\\" .. ply:UniqueID() .. ".txt" )) then
		ply:PrintMessage(HUD_PRINTTALK, "Your money was loaded!
")
		ply:PrintMessage(HUD_PRINTTALK, "You currently have " .. getMoney( ply ) .. "$")
	else
		ply:PrintMessage(HUD_PRINTTALK, "The system didnt find your money file..
Creating..
")
		
		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..
Creating..
")
		
		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..
Creating..
")
		
		file.Write( "GDom\content\data\\" .. ply:UniqueID() .. ".txt", 15 )
		ply:PrintMessage(HUD_PRINTTALK, "Created! You do as default have 15$")	
		return 15
	end
end


Try forward slashes.

Lol, thanks guys :slight_smile: 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(), “:”, “_” )