• Meta Table problems
    2 replies, posted
Hello! I am new to lua and I am trying to make an entity that only a certain team can access. I am trying to use metafunctions to do this. I do not have errors yet since I simply have not done it. It is split up into Init.lua: util.AddNetworkString( "Spec" ) AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "Shared.lua" ) AddCSLuaFile( "Team_SetUp.lua" ) include ( "shared.lua" ) include ( "Team_SetUp.lua" ) local ply = FindMetaTable ( "Player" ) function ENT:Initialize() self:SetModel( "models/hunter/blocks/cube025x025x025.mdl" ) self:PhysicsInit( SOLID_VPHYSICS ) self:SetMoveType( MOVETYPE_VPHYSICS ) self:SetSolid( SOLID_VPHYSICS ) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end end function ENT:Use( active , caller ) if (ply.team == active.team and IsValid( active )) then net.Start ( "Spec" ) net.Send ( "active" ) end end cl_init.lua NOTE: Also how do I find the variable of the player I want to spectate in this code section: include( "Shared.lua" ) function ENT:Draw() self:DrawModel() end net.Receive( "Spec" , function() :Spectate( 4 ) end) I am stuck here from the note above: :Spectate( 4 ) Also Shared.lua ENT.Type = "anim" ENT.Base = "base_gmodentity" ENT.PrintName = "Spectate" ENT.Spawnable = true  ENT.Author = "DramaLlama" team.SetUp ( 0 , "Red" , Color ( 255 , 0 , 0) ) team.SetUp ( 1 , "Blue" , Color ( 0 , 0 , 255) ) and my team system: Team_SetUp.lua local ply = FindMetaTable ( "Player" ) local teams = {}  teams[0] = { name = "Red" , colour = Vector ( 1.0 , 0 , 0 ) } teams[1] = { name = "Blue" , colour = Vector ( 0 , 0 , 1.0 ) } function ply:SetupTeam ( n ) if ( not teams [ n ] ) then return  end self:SetTeam ( n ) self:SetPlayerColor ( teams[ n ].colour ) end I am sorry if I am not clear and ask any questions. Thanks!
This should be posted in the Lua Developer Discussion
I assume you are trying to allow spectating a player if another player presses use on them. There are a few things wrong with your approach. Unlike scripted entities, players don't have a hook function you can define on them to automatically get use events. Instead, you must use a gamemode hook that intercepts ALL use events created by ALL players. http://wiki.garrysmod.com/page/GM/PlayerUse This also runs every tick instead of obeying use types, so you will have to account for this in your hook. Next, when you send a net message, and you want the client to act on some information that you have on the server, you must send that info inside the net message. In this case, you would call net.WriteEntity to send the spectate target, and use net.ReadEntity to get it on the client. This is just what I've seen at first glance. There's probably more.
Sorry, you need to Log In to post a reply to this thread.