Help with include

Hello, i’ve created a lua file called shop, how can I run the function criaMenu() (create a menu) of the .lua file in a sv_ lua and use all the other functions inside of my lua file?

file1.lua



function printHello ()
print( "hello" )
end


file2.lua



include( 'file1.lua' )


Function printHello will now be available in file2.lua as I’ve included file1.lua

In file2 I just call printhello()?

Yep, when you include a file it’s as though everything in that file is inside the one you’ve included it in.

I’m calling a vgui.create in a file called “cl_menu.lua” but I’m receving “attempt do index a nil value ‘vgui’”
Here’s the function



function criaMenu()
	
	print("oi")
	Armas = vgui.Create("DFrame")
	Armas:SetPos(100, 100)
	Armas:SetSize(500, 400)
	Armas:SetTitle("Menu de armas")
	Armas:SetVisible(true)
	Armas:SetDraggable(true)
	Armas:ShowCloseButton(true)
	Armas:MakePopup()
end


Don’t run it server side, put it in a client side file or encase it in if CLIENT then … end or put a if SERVER then return end at the top of your file.

[editline]26th May 2014[/editline]

This means only AddCSLuaFile()ing the file only, server side, and include()ing it client side.

I’ll put this on the sv_ lua?



if CLIENT then
include("cl_menu.lua")
end


Here’s a tutorial on loading files across realms: https://dl.dropboxusercontent.com/u/26074909/tutoring/loading_files_across_realms.lua.html

Here’s a base game-mode which recursively loads files from different directories ( empty except the bare minimum ): https://dl.dropboxusercontent.com/u/26074909/tutoring/_redistributable/basedev_gamemode.rar

If you don’t want to download that, you can look at these: https://dl.dropboxusercontent.com/u/26074909/tutoring/_redistributable/basedev_gamemode/gamemode/cl_init.lua

https://dl.dropboxusercontent.com/u/26074909/tutoring/_redistributable/basedev_gamemode/gamemode/init.lua

https://dl.dropboxusercontent.com/u/26074909/tutoring/_redistributable/basedev_gamemode/gamemode/sh_init.lua

Notice on the sh_init.lua; there is a recursive function to process folders with a callback function, in addition to a special folder modifier ( maps folder only includes the files in InitPostEntity, and adds the OnReloaded for auto-refresh compatibility )

But, if you read down, Shared, Client, Server you’ll see what needs to be done for each such as AddCSLuaFile or include, some cases both are used, other cases not.

Don’t work, here’s the sv_player.lua that will call my cl_menu.lua



// Does the same job as...
if ( SERVER ) then // The server tells the client to download thefile to their computer cache. This needs to be done otherwise the file will not be found on the clientside. 
	AddCSLuaFile( "cl_menu.lua" ); 
	include("cl_menu.lua");
else // This is done on the client side. The server had to AddCSLuaFile it otherwise we couldn't find it. 
	include( "cl_menu.lua" ); 
end 

local PlayerMeta = FindMetaTable("Player")
local EntityMeta = FindMetaTable("Entity")

function GM:PlayerInitialSpawn(ply)
	self:RoundsSetupPlayer(ply)

	ply:SetMoney(10000)

	self:TeamsSetupPlayer(ply)

	if self:GetGameState() != 0 then
		timer.Simple(0, function ()
			if IsValid(ply) then
				ply:KillSilent()
			end
		end)
	end

	self.LastPlayerSpawn = CurTime()

	if self:IsMapVoting() then
		self:NetworkMapVoteStart(ply)
	end
end

function GM:PlayerLoadedLocalPlayer(ply)
end

net.Receive("clientIPE", function (len, ply)
	if !ply.ClientIPE then
		ply.ClientIPE = true
		hook.Call("PlayerLoadedLocalPlayer", GAMEMODE, ply)
	end
end)

function GM:PlayerConnect(name, ip)

end

function GM:PlayerAuthed(ply)
end

function GM:PlayerDisconnected(ply)
	ply:SetTeam(2)
end

util.AddNetworkString("hull_set")
function GM:PlayerSpawn( ply )

	ply:UnCSpectate()

	player_manager.OnPlayerSpawn( ply )
	player_manager.RunClass( ply, "Spawn" )

	hook.Call( "PlayerLoadout", GAMEMODE, ply )
	hook.Call( "PlayerSetModel", GAMEMODE, ply )

	ply:UnDisguise()
	ply:CalculateSpeed()

	ply:SetHMaxHealth(100)
	ply:SetHealth(ply:GetHMaxHealth())

	-- ply:SetCustomCollisionCheck(true)
	GAMEMODE:PlayerSetNewHull(ply)

	self:PlayerSetupHands(ply)

	local col = team.GetColor(ply:Team())
 	local vec = Vector(col.r / 255,col.g / 255,col.b / 255)
 	ply:SetPlayerColor(vec)

 	ply.LastSpawnTime = CurTime()
end

function GM:PlayerSetupHands(ply)
	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
end

function PlayerMeta:CalculateSpeed()
	// set the defaults
	local settings = {
		walkSpeed = 300,
		runSpeed = 50,
		jumpPower = 200,
		canRun = true,
		canMove = true,
		canJump = true
	}

	// speed penalty for small objects (popcan, small bottles, mouse, etc)
	if self:IsDisguised() then
		/*local mul = math.Clamp(self:GetNWFloat("disguiseVolume", 1) / 200, 0.5, 1)
		settings.walkSpeed = settings.walkSpeed * mul*/
		if settings.runSpeed > settings.walkSpeed then
			settings.runSpeed = settings.walkSpeed
		end
		settings.jumpPower = settings.jumpPower * 1.2
	else
		settings.walkSpeed = 200
	end

	hook.Call("PlayerCalculateSpeed", ply, settings)


	// set out new speeds
	if settings.canRun then
		self:SetRunSpeed(settings.runSpeed or 1)
	else
		self:SetRunSpeed(settings.walkSpeed or 1)
	end
	if self:GetMoveType() != MOVETYPE_NOCLIP then
		if settings.canMove then
			self:SetMoveType(MOVETYPE_WALK)
		else
			self:SetMoveType(MOVETYPE_NONE)
		end
	end
	self.CanRun = settings.canRun
	self:SetWalkSpeed(settings.walkSpeed or 1)
	self:SetJumpPower(settings.jumpPower or 1)
end

function GM:PlayerLoadout(ply)
	if ply:Team() == 2 then
		criaMenu()
		ply:Give("weapon_crowbar")
		ply:Give("weapon_shotgun")
		ply:Give("weapon_smg1")
		ply:Give("weapon_frag")
		ply:Give("weapon_357")
		ply:Give("weapon_crossbow")
		ply:GiveAmmo(64, "Buckshot")
		ply:GiveAmmo(255, "SMG1")
		ply:GiveAmmo(48, "357")
		ply:GiveAmmo(8, "XBowBolt")
	end
end

My cl_menu.lua



include("sv_armas.lua")

preco_smg = 10
preco_ar2 = 10
preco_granada = 20
preco_rpg = 100

creditos = {}

function setGrana(id, valor)
	creditos[id] = valor
end	

function getGrana(id)
	return creditos[id]
end

for i = 1 , 64 do
	setGrana(i, 100)
end

function criaMenu()
	
	print("oi")
	Armas = vgui.Create("DFrame")
	Armas:SetPos(100, 100)
	Armas:SetSize(500, 400)
	Armas:SetTitle("Menu de armas")
	Armas:SetVisible(true)
	Armas:SetDraggable(true)
	Armas:ShowCloseButton(true)
	Armas:MakePopup()

	checkSmg = vgui.Create("DCheckBoxLabel", Armas)

	checkSmg:SetText("SMG [" .. preco_smg .. " Créditos]")
	checkSmg:SetPos(10, 40)
	checkSmg:SizeToContents()
	checkSmg:SetValue(0)

	checkAr2 = vgui.Create("DCheckBoxLabel", Armas)
	
	checkAr2:SetText("AR2 [" .. preco_ar2 .. " Créditos]")
	checkAr2:SetPos(10, 70)
	checkAr2:SizeToContents()
	checkAr2:SetValue(0)
	
	checkGranade = vgui.Create("DCheckBoxLabel", Armas)
	
	checkGranade:SetText("Granada [" .. preco_granada .. " Créditos]")
	checkGranade:SetPos(10, 100)
	checkGranade:SizeToContents()
	checkGranade:SetValue(0)	

	checkRPG = vgui.Create("DCheckBoxLabel", Armas)
	
	checkRPG:SetText("RPG [" .. preco_rpg .. " Créditos]")
	checkRPG:SetPos(10, 130)
	checkRPG:SizeToContents()
	checkRPG:SetValue(0)

	lblCredits = vgui.Create("DLabel", Armas)

	lblCredits:SetText("Créditos atuais: " .. creditos[plar])
	lblCredits:SetPos(10, 370)
	lblCredits:SizeToContents()
	
	btConfirmar = vgui.Create("DButton", Armas)

	btConfirmar:SetText("Comprar")
	btConfirmar:SetPos(380, 360)
	btConfirmar:SetSize(100, 26)
	btConfirmar.DoClick = function()
		shopping(LocalPlayer())	
	end
	
	lblCredits:SetText("Créditos atuais: " .. creditos[plar])
end	
function shopping()
	if(checkSmg:GetChecked() and (getGrana[id] - preco_smg) >= 0) then
		darArmas(ply, "weapon_smg1")
		setGrana(id, getGrana(id) - preco_smg)
	end
	if(checkAr2:GetChecked() and (getGrana[id] - preco_ar2) >= 0) then
		darArmas(ply, "weapon_ar2")
		setGrana(id, getGrana(id) - preco_ar2)
	end
	if(checkGranade:GetChecked() and (getGrana[id] - preco_granada) >= 0) then
		darArmas(ply, "weapon_frag")
		setGrana(id, getGrana(id) - preco_granada)
	end
	if(checkRPG:GetChecked() and (getGrana[id] - preco_rpg) >= 0) then
		darArmas(ply, "weapon_rpg")
		setGrana(id, getGrana(id) - preco_rpg)
	end
end

My code is ok, but one more quetion, after click I want that cl_shop.lua update the player money on a get/set file called sv_armas.lua and give a weapon to the player, how can I do that?

If I’m understanding you correctly you’d want to network the serverside variables to the client using the network library.

My menu is like a Shopping, I want to after the buying, update the client money in some way that the money variable still with the value whole game round

When you send the buy command to the server, ensure you’re not taking the clients word for the price. Use an item ID and let the server tell the client how much to pay, etc…

Yes, but my question is how Can I do that? The Shop Menu run on clientside, when I bought something, the server must withdraw that amount from a serverside variable, then give the player all selected Weapons in menu, how can I do that?

Using Nets.
Just like Jeezy said.
Go to this link: http://wiki.garrysmod.com/page/Net_Library_Usage
if you got any trouble understanding, just comment :smiley:

Something like this: https://dl.dropboxusercontent.com/u/26074909/tutoring/networking/networking_booleans.lua.html

Except instead of a boolean, you’d send a number; the server reads the number, looks it up in the item database, check the cash, if enough cash then deduct from player and then add the item.

Seeing a Youtube tutorial, thanks for helping! I’m closing the topic

I’ve made this code to get and set a variable, but in get method the client isn’t receiving the value

Client side



function pagaArmas(valor, arma)
		net.Start("setGrana")
			valores = {LocalPlayer():EntIndex(), valor}
			net.WriteTable(valores)
			net.WriteString(arma)
		net.SendToServer()
end
if(CLIENT) then
		net.Receive("getGrana", function()
		print("Creditos do cliente")
		creditos = net.ReadInt(-2^16)
		end)
end


ServerSide



util.AddNetworkString("setGrana")
util.AddNetworkString("getGrana") 

net.Receive("setGrana", function()
	valores = net.ReadTable()
	id = valores[1]
	creditos[id] = creditos[id] - valores[2]
	print(creditos[id])
	player.GetByID(id):Give(net.ReadString())
	Grana(id)
end)
function Grana(ply) 
	net.Start("getGrana")
		print("Enviando mensagem ao client" .. ply)
		net.WriteInt(-2^16, creditos[ply])
		print("mensagem ao cliente foi enviada " .. ply)
	net.Send(ply)
end