I have recently started to dive into garrysmod gamemode creation following Goature's tutorials, I have got up to the inventory and stats episode, the basic idea is that it will collect short steam id's and create a database of folders with txt files in them containing the data such as money and xp, but for whatever reason I can't get it to create the file, or in my suspicion the code doesn't want to run the function.
The exact errors are, when connecting: [url]http://imgur.com/a3i2hOo[/url]
and when disconnecting: [url]http://imgur.com/RaeujYD[/url]
here is the code:
database.lua(server file):
[CODE]local ply = FindMetaTable("Player")
util.AddNetworkString("database")
function ply:ShortSteamID()
local id = self:SteamID()
local id = tostring(id)
local id = string.Replace(id, "STEAM_0:0:", "") -- removes the Steam_ part of the id
local id = string.Replace(id, "STEAM_0:1:", "") -- removes the Steam_ part of the id
return id -- gives back the id we want
end
-- Nice print Messages about Database
local oldPrint = print
local function print(s)
oldPrint("database.Lua: " .. tostring(s))
end
function ply:databaseDefault()
self:databaseSetValue("money", 100)
self:databaseSetValue("xp", 0)
-- self:databaseSetValue("hunger", 0)
local i = {}
i["soda1"] = {amount = 10}
i["soda2"] = {amount = 10}
self:databaseSetValue("inventory", i)
end
function ply:databaseNetworkedData()
local money = self:databaseGetValue("money")
local xp = self:databaseGetValue("xp")
self:SetNWInt("money", money)
self:SetNWInt("xp", xp)
self:KillSilent()
self:Spawn()
end
function ply:databaseFolders()
return "server/example/players/" .. self:ShortSteamID() .. "/"
end
function ply:databasePath()
return self:databaseFolders() .. "database.txt"
end
function ply:databaseSet(tab)
self.database = tab
end
function ply:databaseGet()
return self.database
end
function ply:databaseCheck()
self.database = {}
local f = self:databaseExists()
if f then
self:databaseRead()
else
self:databaseCreate()
end
self:databaseSend()
self:databaseNetworkedData()
end
function ply:databaseSend()
net.Start("database")
net.WriteTable(self:databaseGet())
net.Send(self)
end
function ply:databaseExists()
local f = file.Exists(self:databasePath(), "DATA")
return f
end
function ply:databaseRead()
local str = file.Read(self:databasePath(), "DATA")
self:databaseSet(util.KeyValuesToTable(str))
end
function ply:databaseSave()
local str = util.TableToKeyValues(self:database)
local f = file.Write(self:databasePath(), str)
self:databaseSend()
end
function ply:databaseCreate()
self:databaseDefault()
local b = file.CreateDir(self:databaseFolders())
print("Made directory!")
self:databaseSave()
end
function ply:databasePlayerDisconnect()
self:databaseSave()
end
function ply:databaseSetValue(name, v)
if not v then return end
if type(v) == "table" then
if name == "inventory" then
for k,b in pairs(v) do
if b.amount <= 0 then
v[k] = nil
end
end
end
end
local d = self:databaseGet()
d[name] = v
self:databaseSave()
end
function ply:databaseGetValue(name)
local d = self.databaseGet()
return d[name]
end[/CODE]
the Client side database code(cl_database.lua):
[CODE]local database = {}
local function databaseReceive(tab)
database = tab
end
net.Receive("database", function(len)
local tab = net.ReadTable()
databaseReceive(tab)
end)
function databaseTable()
return database
end
function databaseGetValue(name)
local d = databaseTable()
return d[name]
end[/CODE]
and finally the init.lua:
[CODE]AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
AddCSLuaFile("database/cl_database.lua")
include("shared.lua")
include("player.lua")
include("database/database.lua")
-- function GM:PlayerConnect( name, ip )
-- end
function GM:PlayerInitialSpawn( ply )
PrintMessage(HUD_PRINTTALK, "player: " .. ply:Nick() .. " has joined the game")
ply:SetGamemodeTeam( 0 ) -- Refers to player.lua
end
function GM:PlayerSpawn( ply )
ply:SetModel("models/player/group01/male_07.mdl") -- sets the model when they spawn
ply:GiveGamemodeWeapons() -- should give them weapons but is not working, code in player.lua
-- [[ To fix the invisible hands in firstPerson ]] --
local oldhands = ply:GetHands()
if ( IsValid( oldhands ) ) then oldhands:Remove() end
local hands = ents.Create( "gmod_hands" )
if ( IsValid( hands ) ) then
ply:SetHands( hands )
hands:SetOwner( ply )
-- Which hands should we use?
local cl_playermodel = ply:GetInfo( "cl_playermodel" )
local info = player_manager.TranslatePlayerHands( cl_playermodel )
if ( info ) then
hands:SetModel( info.model )
hands:SetSkin( info.skin )
hands:SetBodyGroups( info.body )
end
-- Attach them to the viewmodel
local vm = ply:GetViewModel( 0 )
hands:AttachToViewmodel( vm )
vm:DeleteOnRemove( hands )
ply:DeleteOnRemove( hands )
hands:Spawn()
end
-- [[ Fixed ]] --
end
function GM:PlayerAuthed( ply, steamID, uniqueID )
ply:databaseCheck()
end
function GM:PlayerDisconnected(ply)
ply:databasePlayerDisconnect()
end[/CODE]
As you can see the init.lua includes the database file but doesnt want to access its functions for some reason, also the database files are located in a folder called database.
Hey welcome to the forum!
[url=http://facepunch.com/forums/65]The Garrysmod Developer Discussion is that way![/url]
In the future it may be advantageous to read the Sticky at the top of each forum before posting to get the rules and guidelines, as well as to make sure you're in the right place!
Sorry, you need to Log In to post a reply to this thread.