This is a bit more than a newbie question, mainly because gmod code, gmod wiki, and gmod seem to be wrong/broken. (I'm betting it's me though.) Everytime I execute the following:
[lua]
function BloodSplat()
local trace = self.Owner:GetEyeTrace()
local effectdata = EffectData()
effectdata.SetOrigin( trace.HitPos )
effectdata.SetNormal( trace.HitNormal )
effectdata.SetScale( 1 )
util.Effect( "BloodImpact", effectdata )
// [debug]
if self.Owner:IsAdmin() or self.Owner:IsSuperAdmin() then
chat.AddText(self.Owner, Color(100,255,100), ": [debug] Blood Sprayed." )
end
// [/debug]
return end
[/lua]
I get the error, "attempt to index global 'self' (a nil value).
I've done everything exaclty like the wiki, yet still I get this error. And it worn't render any effects on client only. (by adding 'if CLIENT then .. end' around it. None of the wikis add the client wrapper, what do I do? Btw, it's in shared.lua
[url]http://wiki.garrysmod.com/?title=Util.Effect[/url]
[url]http://wiki.garrysmod.com/?title=Garry's_Example_SWEP[/url]
To understand why that's wrong, here's a brief explanation of "self":
In Lua, there's two main ways to use/declare a function on a table or object: with a colon or with a period.
[lua]Player.Func()
-- or
Player:Func()[/lua]
When you use a colon in a function call, what happens is your function gets an extra argument entitled "self", which is the object you're calling the function on. So,
[lua]SWEP:Func()
print( self )
end[/lua]
self is the SWEP entity, and when you call SWEP:Func() it would print the weapon entity name and all that. So, getting to the point your problem is that your function is just a random global table in space -- you want to declare it on the SWEP object, like SWEP:BloodSplat or something.
tl;dr Change BloodSplat to SWEP:BloodSplat.
Note: Wiki isn't wrong, you're just not doing it right.
I thought I was wrong, Thanks.
[editline]12:14AM[/editline]
Ok now I'm getting another error:
bad argument #2 to 'SetOrigin' (Vector Expected, got no value)
[lua]
local trace = self.Owner:GetEyeTrace()
local effectdata = EffectData()
effectdata.SetOrigin( trace.HitPos )
effectdata.SetNormal( trace.HitNormal )
effectdata.SetScale( 1 )
util.Effect( "BloodImpact", effectdata )
[/lua]
I've got no idea what's going on (again). trace.HitPos, according to the wiki, should be outputting a vector. I've looked at EVERYTHING and tried this code, NPCS, Walls, Glass, Props, Sweps, everything. I've got no idea what's going on.
You need to call it like this:
[code]
effectdata:SetOrigin( trace.HitPos )
[/code]
Notice the colon.
Ah, ok, thank you.
Another error:
'Attempt to call global 'SWEP:' (a nil value)
Here's the function.
[lua]
function SWEP:BloodSplat( trace )
local effectdata = EffectData()
effectdata:SetOrigin( trace.HitPos )
effectdata:SetNormal( trace.HitNormal )
effectdata:SetScale( 1 )
util.Effect( "BloodImpact", effectdata )
return
end
[/lua]
Function is called in 'function SWEP:PrimaryAttack()'
And yes, SWEP:BloodSplat is defined before SWEP:PrimaryAttack
Sorry, you need to Log In to post a reply to this thread.