So I am trying make a table from a file, but I am not sure how to. I have already put all the data I need into a .txt file, and I need help making a table from that.
Here is my current code:
[LUA]
g = file.Read("allk.txt")
gtable = [g]
for i = 0, i + 1, i > 100
table.insert(gtable)
end
[/LUA]
What do you want to separate it by? Characters? Paragraphs? Newlines?
[editline]19th July 2016[/editline]
Also, [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/util/TableToJSON]util.TableToJSON[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/util/JSONToTable]util.JSONToTable[/url] might prove helpful depending on what you're doing
[QUOTE=MPan1;50736673]What do you want to separate it by? Characters? Paragraphs? Newlines?
[editline]19th July 2016[/editline]
Also, [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/util/TableToJSON]util.TableToJSON[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/util/JSONToTable]util.JSONToTable[/url] might prove helpful depending on what you're doing[/QUOTE]
new lines
I think the easiest way would be
[CODE]
local g = file.Read("allk.txt")
local tbl = string.Explode( '\n', g, false )
[/CODE]
Alright, now I am trying to give the player a weapon based on information from the table. Right now it gives me this error:
[LUA]
attempt to call method 'Give' (a nil value)
[/LUA]
Help?
[QUOTE=obesewhale;50736769]Alright, now I am trying to give the player a weapon based on information from the table. Right now it gives me this error:
[LUA]
attempt to call method 'Give' (a nil value)
[/LUA]
Help?[/QUOTE]
Could you post the code please?
[QUOTE=MPan1;50736772]Could you post the code please?[/QUOTE]
[LUA]
skins = file.Read("allknives.txt")
tbl = string.Explode( '\n', skins, false )
recievedskin = table.Random( tbl )
RunConsoleCommand("GiveMeTheGun")
keys = keys - 1
file.Write( "keys.txt", keys )
end
end
end )
concommand.Add("GiveMeTheGun", function(ply, command, arguments)
ply:Give( recievedskin )
ply:PrintMessage( HUD_PRINTTALK, "Recieved"..recievedskin.."!" )
end)
[/LUA]
Make sure it's serverside
[QUOTE=crazyscouter;50736799]Make sure it's serverside[/QUOTE]
the console command?
[editline]19th July 2016[/editline]
[QUOTE=crazyscouter;50736799]Make sure it's serverside[/QUOTE]
I tried moving it to the shared.lua file, but it gave me the same error
[QUOTE=obesewhale;50736810]I tried moving it to the shared.lua file, but it gave me the same error[/QUOTE]
Shared doesn't mean serverside- it means shared- client AND server. Move it serverside and hopefully the error will stop
[QUOTE=MPan1;50736851]Shared doesn't mean serverside- it means shared- client AND server. Move it serverside and hopefully the error will stop[/QUOTE]
alright
[editline]19th July 2016[/editline]
welp. I am at a loss. This is all my code:
init.lua
[LUA]
if SERVER then
include("sh_init.lua")
end
if CLIENT then
include("cl_init.lua")
end
ply = LocalPlayer()
if ply.DarkRPVars.AFK then
timer.Pause("KeyTimer")
else
timer.UnPause("KeyTimer")
end
[/LUA]
cl_init.lua
[LUA]
include("shared.lua")
ply = LocalPlayer()
if ply:IsUserGroup("VIP") then
WalletMax = 15
else
if ply:IsUserGroup("VIP+") then
WalletMax = 20
else
WalletMax = 10
end
end
function SetupDataTables()
self:NetworkVar( "String", 1, "keys")
self:NetworkVar( "String", 2, "WalletMax")
end
keys = file.Read("keys.txt")
if keys == nil then
keys = 0
file.Write( "keys.txt", keys )
end
if keys != 0 then
keys = file.Read("keys.txt")
end
function KeyTimerFunc()
timer.Create( "KeyTimer", 1, 0, 0, AddKey())
end
function AddKey()
--default time is 3 hours, or 216000
if keys == WalletMax then
Entity( 1 ):PrintMessage( HUD_PRINTTALK, "Key Wallet Full!" )
timer.Stop("KeyTimer")
else
keys = keys + 1
timer.Start("KeyTimer")
file.Write( "keys.txt", keys )
end
end
hook.Add( "Initialize", "Key Timer", KeyTimerFunc )
hook.Add( "HUDPaint", "HelloThere", HudDraw)
function HudDraw()
draw.DrawText( keys.." Keys", "TargetID", 50, 15, Color( 0, 0, 0, 255 ), TEXT_ALIGN_CENTER )
draw.RoundedBox( 10, 20, 15, 60, 25, Color( 0, 0, 0, 100 ) ) -- Draw a blue button
end
concommand.Add( "AddKeys", function()
if keys == WalletMax then
Entity( 1 ):PrintMessage( HUD_PRINTTALK, "Key Wallet Full!" )
else
keys = keys + 1
file.Write( "keys.txt", keys )
end
end )
concommand.Add( "AddFiveKeys", function()
if keys == WalletMax then
Entity( 1 ):PrintMessage( HUD_PRINTTALK, "Key Wallet Full!" )
else
keys = keys + 5
if keys > WalletMax then
keys = keys - 5
end
file.Write( "keys.txt", keys )
end
end )
concommand.Add( "OpenMenu", function()
local Frame = vgui.Create( "DFrame" )
Frame:SetTitle( "Advanced Knife System" )
Frame:SetSize( 300, 300 )
Frame:Center()
Frame:MakePopup()
Frame.Paint = function( self, w, h ) -- 'function Frame:Paint( w, h )' works too
draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 255 ) ) -- Draw a red box instead of the frame
end
local Button = vgui.Create( "DButton", Frame )
Button:SetText( "Use Key" )
Button:SetTextColor( Color( 255, 255, 255 ) )
Button:SetPos( 100, 100 )
Button:SetSize( 100, 30 )
Button.Paint = function( self, w, h )
draw.RoundedBox( 0, 0, 0, w, h, Color( 41, 128, 185, 250 ) ) -- Draw a blue button
end
Button.DoClick = function()
if keys == 0 then
Entity( 1 ):PrintMessage( HUD_PRINTTALK, "Not enough keys." )
else
Entity( 1 ):PrintMessage( HUD_PRINTTALK, "Used 1 Key!" )
skins = file.Read("allknives.txt")
tbl = string.Explode( '\n', skins, false )
recievedskin = table.Random( tbl )
RunConsoleCommand("GiveMeTheGun")
keys = keys - 1
file.Write( "keys.txt", keys )
end
end
end )
[/LUA]
sh_init.lua
[LUA]
//hi
concommand.Add("GiveMeTheGun", function(ply, command, arguments)
skins = file.Read("allknives.txt")
tbl = string.Explode( '\n', skins, false )
recievedskin = table.Random( tbl )
RunConsoleCommand("GiveMeTheGun")
keys = file.Read("keys.txt")
if keys == nil then
keys = 0
file.Write( "keys.txt", keys )
end
if keys != 0 then
keys = file.Read("keys.txt")
end
keys = keys - 1
file.Write( "keys.txt", keys )
ply:Give( recievedskin )
ply:PrintMessage( HUD_PRINTTALK, "Recieved"..recievedskin.."!" )
end)
[/LUA]
shared.lua
EMPTY
please help. Whenever I open the menu, I click the button, and then my game crashes. I give up.
Sorry, you need to Log In to post a reply to this thread.