I have been trying to make an object that only spectates the team of the creator.
I keep getting this:
attempt to call method 'Team' (a nil value)
I understand that meta tables are not the best way forward for this but I am close already and I need to get past this problem.
If you want to ask any questions then feel free.
Thanks!
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "Shared.lua" )
include ( "Shared.lua" )
local meta = FindMetaTable ( "Player" )
function meta:SameTeam()
if ( OwnTm == CallTm ) then
return true
else
return false
end
end
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 )
local Own = self:GetOwner()
local OwnTm = Own:Team()
local CallTm = caller:Team()
local ply = meta
if ( ply:SameTeam() == true ) then
caller:Spectate( 4 )
end
end
include ( "Shared.lua" )
local meta = FindMetaTable ( "Player" )
You do know that you cant do a space between the function name and the brackets, right?
By doing that you tell lua to search for a variable named include and FindMetaTable, and not running a function
Or i am entirely wrong right now but that would surprise me right now.
I never thought of that I just thought it was all about the look of things. It did not change anything for me.
Surprise! You're wrong. Lua cares very little about whitespace. You can insert whitespace just about anywhere, and as long as you aren't breaking up keywords, variable names, or string literals, the script will work exactly the same.
Most programming languages tend towards this attitude on whitespace. Python is a notable exception, as it uses whitespace as a replacement for block end markers.
There's no need to make a function for a single if statement. Just do "OwnTm == "CallTm" in the ENT:Use function. The function you made wouldn't work the way you want it to either, it would always return true because nil will always equal nil. I suggest you learn about scoping and how it works then learn how metatables work.
Lua 5.1 Reference Manual - Scoping
Lua 5.1 Reference Manual - Metatables
If there's anything else you want to learn you can always check the Lua Reference Manual.
https://i.imgur.com/icsTXNq.png
Thank you! Meta tables do confuse me.
Sorry, you need to Log In to post a reply to this thread.