script not loading

hi again FP. I have

include( “playerdata.lua” )

in my init.lua for a gamemode, but this script will not run. as in, no lua errors, it just won’t work. none of the Msg’s i have below show up in console. Is this the correct way to make a SQL db?

[lua]
function tables_exist()
if (sql.TableExists(“player_info”) then --check if tables exist, if not, create them
Msg(“Table exists.”)
else
sql.Query = “CREATE TABLE player_info ( unique_id varchar(255), money int, hazflashlight int )” --create table and columns
–result = sql.Query(query)
if (sql.TableExists(“player_info”)) then --if it worked
Msg("Succes! Player table created.
")
else
Msg("Somthing went while creating table player_info!
") --if it didn’t work
Msg( sql.LastError() … "
" )
end
end
end

function PlayerInitialSQLSpawn( ply )

timer.Create("Steam_id_delay", 2, 1, function( ply )	
--Sets up a delay cause otherwise the steamid wont be available yet
SteamID = ply:SteamID()
--Sets the variable SteamID to the players steamID
ply:SetNWString("SteamID", SteamID)
--Networks the variable so we can use it everywhere	
--Creates a timer loop that repeats infinite every 10 secconds to save our stats (We get into that later)
player_exists( ply )					
--Calls the player exists function
end) 

end

function player_exists ( ply )
steamID = ply:GetNWString(“SteamID”)
result = sql.Query(“SELECT unique_id, money FROM player_info WHERE unique_id = '”…steamID…"’")
if (result) then --see if that entry EXISTS

else
	new_player( steamID, ply ) --Create a new player
end

end

function new_player( SteamID, ply ) --make a new player in the table
steamID = SteamID
sql.Query( “INSERT INTO player_info (unique_id, money, hazflashlight)VALUES (’”…steamID…"’, ‘1000’, ‘0’)" ) --set default values
result = sql.Query( “SELECT unique_id, money FROM player_info WHERE unique_id = '”…steamID…"’" )
if (result) then

else
	Msg("Something went wrong with creating a new players info!")
end

end

function Initialize()
tables_exist()
end
–Msg(“WTF SCRIPT LOADED”)
hook.Add( “InitPostEntity”, “initialize”, Initialize )
hook.Add( “PlayerInitialSpawn”, “PlayerInitialData”, PlayerInitialSQLSpawn )
[/lua]

Wow, so much mess.

Is this your coding?

If so, first, start out by making every single one of those a local function.

Also, there are errors. Errors will show up in your console before your player even spawns, showing you if a script has failed to initialize.

Ok, I made them all local and located a missing parenthesis. (didn’t see the error initially)

I now have the problem of the timer function not passing the ply variable on.

[lua]
timer.Create(“Steam_id_delay”, 2, 1, function( ply )
–Sets up a delay cause otherwise the steamid wont be available yet
SteamID = ply:SteamID()
–Sets the variable SteamID to the players steamID
ply:SetNWString(“SteamID”, SteamID)
–Networks the variable so we can use it everywhere
player_exists( ply )
–Calls the player exists function
end)
[/lua]

dumps an lua error in my console:

Timer Error: ShadowRP\gamemode\playerdata.lua:22: attempt to index local ‘ply’ (a nil value)

line 22: SteamID = ply:SteamID()

Replace

[lua]
timer.Create(“Steam_id_delay”, 2, 1, function( ply )
–Sets up a delay cause otherwise the steamid wont be available yet
SteamID = ply:SteamID()
–Sets the variable SteamID to the players steamID
ply:SetNWString(“SteamID”, SteamID)
–Networks the variable so we can use it everywhere
–Creates a timer loop that repeats infinite every 10 secconds to save our stats (We get into that later)
player_exists( ply )
–Calls the player exists function
end)
[/lua]

with

[lua]
timer.Create(“Steam_id_delay”, 2, 1, function( ply )
–Sets up a delay cause otherwise the steamid wont be available yet
local SteamID = ply:SteamID()
–Sets the variable SteamID to the players steamID
ply:SetNWString(“SteamID”, SteamID)
–Networks the variable so we can use it everywhere
–Creates a timer loop that repeats infinite every 10 secconds to save our stats (We get into that later)
player_exists( ply )
–Calls the player exists function
end, ply) – We need to pass in the ply-argument
[/lua]

because you need to pass in the arguments to the function inside the timer (take a look at the last line on both scripts). You might also want to learn to use local-variables properly.

oh ok, thanks.

yeah I wasn’t sure if the variable i was making into a network var had to be global or not.

Just remember that any variable which is only used inside a single scope can be made local. In functions like SetNWString the variable is passed as value, not by reference, so it doesn’t matter whether it’s local or not.

Why not use PData instead of having to create/update tables?

-snip-

I didn’t think there was a way to change databases using the built in “sql” engine?