Error Help "function arguments expected near '~='"
12 replies, posted
Hey All,
Ive got a error with this piece of code:
[lua]
function ArmorSpawn(ply)
local armorpercent = ( client:Armor() / 100 )
if ply:armorpercent ~= 100 then --- Line of error
local bdemo = ents.Create( 'bodyarmor' );
bdemo:SetOwner( ply );
bdemo:SetModel("models/vest.mdl")
bdemo:Spawn( );
bdemo:Activate( );
end
if ply:armorpercent ~= 0 then
bdemo:Remove( );
end
end
[/lua]
And here is the error:
[code]
Testing\gamemode\Init.lua:189: function arguments expected near '~='
[/code]
Ive been looking through the Wiki and examples on gmod.org to see how to fix the problem, if anyone can point me in the right direction it is much appreciated,
Thanks for reading.
if client:Armor() / 100 ~= 100 then
[QUOTE=CombineGuru;19644661]if client:Armor() / 100 ~1 100 then[/QUOTE]
Thanks CombineGuro Ill test now
The problem is that you defined the variable 'armorpercent', but you are trying to call the function 'ply.armorpercent'. So, change line 3 to:
[lua]if armorpercent ~= 100 then[/lua]
[QUOTE=Overv;19644761]The problem is that you defined the variable 'armorpercent', but you are trying to call the function 'ply.armorpercent'. So, change line 3 to:
if armorpercent ~= 100 then[/QUOTE]
Oh I see, Would this still be global to just one player, also Im just trying to add it to a 'Think' hook would this code work well with that or should I use something else?
[editline]04:58PM[/editline]
[QUOTE=Jazora;19644920]Oh I see, Would this still be global to just one player, also Im just trying to add it to a 'Think' hook would this code work well with that or should I use something else?[/QUOTE]
Ive got a error now that It wont pickup "client" so I changed it to "ply" and still no luck, here is the code:
[lua]
function ArmorSpawn(ply)
local armorpercent = ( ply:Armor() / 100 )
if armorpercent ~= 100 then
local bdemo = ents.Create( 'bodyarmor' );
bdemo:SetOwner( ply );
bdemo:SetModel("models/vest.mdl")
bdemo:Spawn( );
bdemo:Activate( );
end
end
hook.Add("Think", "ArmorSpawn", ArmorSpawn)
[/lua]
And here is the error:
[code]
Hook 'ArmorSpawn' Failed: Testing\gamemode\Init.lua:188: attempt to index local 'ply' (a nil value)
[/code]
Anyone know what I should change it to?
Think doesn't have a player argument, I think you want a loop:
[lua]function ArmorSpawn()
for _, ply in pairs( player.GetAll() ) do
-- Do your stuff
end
end[/lua]
[QUOTE=Overv;19645262]Think doesn't have a player argument, I think you want a loop:
function ArmorSpawn()
for _, ply in pairs( player.GetAll() ) do
-- Do your stuff
end
end[/QUOTE]
Thanks Overv for the help Ill give it a try and post results :D
[editline]05:34PM[/editline]
[QUOTE=Jazora;19645288]Thanks Overv for the help Ill give it a try and post results :D[/QUOTE]
Well I saw no error messages at all so thats fine, my only problem is its not calling me enity, did I need to add a hook or does loop do it automatically, to see how my entity is based on, it uses the same code as the Headcrab hat if that has something to do with it?
Code:
[lua]
function ArmorSpawn()
for _, ply in pairs( player.GetAll() ) do
local armorpercent = ( ply:Armor() / 100 )
if armorpercent ~= 100 then
local bdemo = ents.Create( 'bodyarmor' );
bdemo:SetOwner( ply );
bdemo:SetModel("models/vest.mdl")
bdemo:Spawn( );
bdemo:Activate( );
end
end
end
[/lua]
On an unrelated note - your code makes it so that if any player's armor [b]percent[/b] is not 100 (aka 10000 armor), then it'll spawn the ent.
Change it to armorpercent ~= 1.
Also, be warned - you're spawning it every think call. You might want to rename it "ply.bdemo" and check to see if ply.bdemo already exists before spawning it.
[QUOTE=Disseminate;19645825]On an unrelated note - your code makes it so that if any player's armor [B]percent[/B] is not 100 (aka 10000 armor), then it'll spawn the ent.
Change it to armorpercent ~= 1.
Also, be warned - you're spawning it every think call. You might want to rename it "ply.bdemo" and check to see if ply.bdemo already exists before spawning it.[/QUOTE]
Oh I can see now why it wasnt spawning, Ill give it a quick try and post results Thanks Disseminate :)
[editline]06:23PM[/editline]
[QUOTE=Jazora;19645852]Oh I can see now why it wasnt spawning, Ill give it a quick try and post results Thanks Disseminate :)[/QUOTE]
Hmmm it appears to still not be spawing, I get no error messages. Here is the code:
[lua]
function ArmorSpawn()
for _, ply in pairs( player.GetAll() ) do
local armorpercent = ( ply:Armor() / 100 )
if armorpercent ~= 1 then
local bdemo = ents.Create( 'bodyarmor' );
ply.bdemo:SetOwner( ply );
ply.bdemo:SetModel("models/vest.mdl")
ply.bdemo:Spawn( );
ply.bdemo:Activate( );
end
end
end
[/lua]
Anyone got any ideas on how to make it spawn? It uses the same code as this:
[url]http://www.garrysmod.org/downloads/?a=view&id=68641[/url]
[lua]
function ArmorSpawn()
for _, ply in pairs( player.GetAll() ) do
local armorpercent = ( ply:Armor() / 100 )
if armorpercent ~= 1 then
local bdemo = ents.Create( 'bodyarmor' );--I'm pretty sure this should be ents.Create( "ent_bodyarmor" )
ply.bdemo:SetOwner( ply );
ply.bdemo:SetModel("models/vest.mdl")
ply.bdemo:Spawn( );
ply.bdemo:Activate( );
end
end
end
[/lua]
[editline]11:11PM[/editline]
I'm pretty sure this should be ents.Create( "ent_bodyarmor" )
Also there is a TYPO on line 5:
ents.Create( 'bodyarmor' ) should be ents.Create([b] "[/b]ent_bodyarmor[b]"[/b] )
[QUOTE=Chad Mobile;19651152] function ArmorSpawn() for _, ply in pairs( player.GetAll() ) do local armorpercent = ( ply:Armor() / 100 ) if armorpercent ~= 1 then local bdemo = ents.Create( 'bodyarmor' );--I'm pretty sure this should be ents.Create( "ent_bodyarmor" ) ply.bdemo:SetOwner( ply ); ply.bdemo:SetModel("models/vest.mdl") ply.bdemo:Spawn( ); ply.bdemo:Activate( ); end end end
[editline]11:11PM[/editline]
I'm pretty sure this should be ents.Create( "ent_bodyarmor" )
Also there is a TYPO on line 5:
ents.Create( 'bodyarmor' ) should be ents.Create([B] "[/B]ent_bodyarmor[B]"[/B] )[/QUOTE]
Thanks Chad Ill give it a try :)
[editline]12:17AM[/editline]
[QUOTE=Jazora;19652243]Thanks Chad Ill give it a try :)[/QUOTE]
Hmmm I tried it again, No errors still not spawning a entity, Am I missing something that it needs in order to spawn?
Here is the Init file for the entity:
[lua]
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')
function ENT:SpawnFunction( pl , tr )
local headcrabs , m = ents.FindByClass( "bodyarmor", "conehat" );
for _ , m in pairs( headcrabs ) do
if ( m && m:IsValid( ) && m:GetOwner( ) == pl ) then
return;
end
end
local eyes = pl:LookupAttachment( 'chest' );
if ( chest == 0 ) then return; end
local bdemo = ents.Create( 'bodyarmor' );
bdemo:SetOwner( pl );
bdemo:SetParent( pl );
bdemo:SetModel("models/vest.mdl")
bdemo:Spawn( );
bdemo:Activate( );
bdemo:SetMoveType( MOVETYPE_NONE )
bdemo:SetSolid( SOLID_NONE )
bdemo:SetCollisionGroup( COLLISION_GROUP_NONE )
bdemo:DrawShadow( false )
return bdemo;
end
function ENT:Think()
if self.Entity:GetOwner():GetViewEntity():GetClass() == "gmod_cameraprop" then
self.Entity:GetOwner():SetNWBool( "Camera", true)
else
self.Entity:GetOwner():SetNWBool( "Camera", false)
end
end
[/lua]
Any idea anyone?
[lua]
function ENT:SpawnFunction( ply, tr )
if ( !tr.Hit ) then return end
local ent = ents.Create("ent name")
ent:SetPos( tr.HitPos + tr.HitNormal * 16 )
ent:Spawn()
ent:Activate()
return ent
end
[/lua]
Throw that somewhere in the middle. Check the code too!
Sorry, you need to Log In to post a reply to this thread.