I am making the base for a weapon that (right now) launches you up. In the [CODE]SWEP:Initialize()[/CODE] and [CODE]SWEP:Reload()[/CODE] (because running out of fuel removes the hook) functions, this script is executed-
[CODE]
print("Mash the jump button to accelerate!") --On equip, tell user how to use.
hook.Add( "KeyPress", "keypress_rocket", function( ply, key )
if ( key == IN_JUMP ) then -- When the jump key is pressed...
ply:SetVelocity( ply:GetVelocity() + Vector( 0, 0, 10 ) ) --Set velocity to up 10 than it allready is
ply:EmitSound("jetpackfire.wav", 400, 400 ) -- Emit jetpack sound from weapon
Fuel = Fuel - 1
self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK) -- START FLAILING YOUR ARMS AROUND!!! (ACT_VM_PRIMARYATTACK is an arm flail :D)
end
end )
if true then
print("Initialized.")
else
print("Sorry, the SWEP failed to initialize. If there are console errors, send them to Lavacoal123 on steam!")
end
[/CODE]
Problem- CONSOLE ERRORS!
[CODE]
addons/coal's jetpacks/lua/weapons/basicjetpack/shared.lua:64: attempt to index field 'Weapon' (a nil value)
1. v - addons/coal's jetpacks/lua/weapons/basicjetpack/shared.lua:64
2. old_hook_call - lua/includes/modules/hook.lua:84
3. unknown - lua/includes/modules/momo/compat.lua:23
[/CODE]
This is the 64th line:
[CODE]
self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
[/CODE]
Side notes, YES I defined Fuel before it is mentioned in this function and YES I made the SMD and QC file for the viewmodel and YES jetpackfire.wav DOES exist.
Post the whole weapon file and the error.
[QUOTE=code_gs;50711349]Post the whole weapon file and the error.[/QUOTE]
I also added some stuff like the error to the top :smile:
Here-
[CODE]
//JetPack SWEP Script by Lavacoal123
SWEP.DrawCrosshair = false
SWEP.Weight = 5
SWEP.ViewModel = "models/coal/jetpackstuff/jetpack_v.mdl"
SWEP.WorldModel = "models/weapons/w_fists.mdl"
SWEP.HoldType = "Pistol"
SWEP.ViewModelFOV = 64
SWEP.Slot = 0
SWEP.Purpose = "Shoot yourself high in the sky!"
SWEP.AutoSwitchTo = true
SWEP.Contact = "coalstudios95@gmail.com"
SWEP.Author = "Lavacoal123"
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
SWEP.SlotPos = 0
SWEP.Instructions = "Jump while equipped"
SWEP.base = "weapon_base"
SWEP.Category = "Coal's SWEPs"
SWEP.PrintName = "Basic Jetpack"
Fuel = 35
Used = 0
concommand.Add("jetfuel", function(ArgStr)
print("Your jetpack fuel is " .. Fuel .. " Out of 35!")
if ( ArgStr == "used" ) then
Used = 35 - Fuel
print("So far, you used " .. Used .. " Units of fuel.")
end
end )
function SWEP:Think()
--Sets current health to a very high number when equipped.
self.Owner:SetHealth(999999999999)
if ( Fuel == 0 ) then
hook.Remove( "KeyPress", "keypress_rocket" )
end
end
function SWEP:Reload()
Fuel = 35
print("Fully reloaded.")
hook.Add( "KeyPress", "keypress_rocket", function( ply, key )
if ( key == IN_JUMP ) then -- When the jump key is pressed...
ply:SetVelocity( ply:GetVelocity() + Vector( 0, 0, 10 ) ) --Set velocity to up 10 than it allready is
ply:EmitSound("jetpackfire.wav", 400, 400 ) -- Emit jetpack sound from weapon
Fuel = Fuel - 1
self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK) -- START FLAILING YOUR ARMS AROUND!!!
end
end )
if true then
print("Fully reloaded.")
else
print("Sorry, the SWEP failed to reload. If there are console errors, send them to Lavacoal123 on steam!")
end
end
function SWEP:Initialize()
print("Mash the jump button to accelerate!") --On equip, tell user how to use.
hook.Add( "KeyPress", "keypress_rocket", function( ply, key )
if ( key == IN_JUMP ) then -- When the jump key is pressed...
ply:SetVelocity( ply:GetVelocity() + Vector( 0, 0, 10 ) ) --Set velocity to up 10 than it allready is
ply:EmitSound("jetpackfire.wav", 400, 400 ) -- Emit jetpack sound from weapon
Fuel = Fuel - 1
self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK) -- START FLAILING YOUR ARMS AROUND!!!
end
end )
if true then
print("Initialized.")
else
print("Sorry, the SWEP failed to initialize. If there are console errors, send them to Lavacoal123 on steam!")
end
end
[/CODE]
And the mod folder has [URL="https://www.mediafire.com/?out3rg70u3v17e0"]these[/URL]:
lua/weapons/basicjetpack/shared.lua
models/coal/jetpackstuff/jetpack_v.mdl
models/coal/jetpackstuff/jetpack_v.dx80.vtx
models/coal/jetpackstuff/jetpack_v.dx90.vtx
models/coal/jetpackstuff/jetpack_v.sw.vtx
models/coal/jetpackstuff/jetpack_v.vvd
(No materials are used in the veiwmodel and the worldmodel right now is a pistol XD)
sound/jetpackfire.wav
1.) Don't use globals for the fuel/used variables.
[CODE]SWEP.PrintName = "Basic Jetpack"
Fuel = 35
Used = 0[/CODE]
2.) Why are you doing this again?
[CODE] if true then
print() [/CODE]
3.) That emits sounds from the player, also don't use EmitSound for loops <3
[CODE] ply:EmitSound("jetpackfire.wav", 400, 400 )[/CODE]
4.) I think KeyPress hook might be called when the weapon is not really a thing just yet (not so sure about that one), add validity checks (IsValid() etc.) inside the hook for fail safes, additionally put debug messages if those validity checks return false.
[CODE]hook.Add( "KeyPress", "keypress_rocket", function( ply, key )
if ( key == IN_JUMP ) then -- When the jump key is pressed...
if ( IsValid(self.Weapon) ) then -- When our entity exists
ply:SetVelocity( ply:GetVelocity() + Vector( 0, 0, 10 ) ) --Set velocity to up 10 than it allready is
ply:EmitSound("jetpackfire.wav", 400, 400 ) -- Emit jetpack sound from weapon
Fuel = Fuel - 1
self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK) -- START FLAILING YOUR ARMS AROUND!!!
else
print("We've got trouble!")
end
end
end )[/CODE]
[QUOTE=Kiro The Pro;50711486]1.) Don't use globals for the fuel/used variables.
[CODE]SWEP.PrintName = "Basic Jetpack"
Fuel = 35
Used = 0[/CODE]
2.) Why are you doing this again?
[CODE] if true then
print() [/CODE]
3.) That emits sounds from the player, also don't use EmitSound for loops <3
[CODE] ply:EmitSound("jetpackfire.wav", 400, 400 )[/CODE]
4.) I think KeyPress hook might be called when the weapon is not really a thing just yet (not so sure about that one), add validity checks (IsValid() etc.) inside the hook for fail safes, additionally put debug messages if those validity checks return false.[/QUOTE]
1.) KK
2.) I just want to know if its working
3.) Yeah, I know, that was an accident but thx
4.) Nice, I think that's why the script was giving me the [CODE]attempt to index field 'Weapon' (a nil value)[/CODE] error, because Weapon isn't defined yet, thanks
[editline]14th July 2016[/editline]
Question about [URL="http://wiki.garrysmod.com/page/Global/IsValid"]IsValid[/URL]: if it returns a Boolean variable, then do you use it like this?
[CODE]
if IsValid(Weapon) == true then
--Hook stuff
else
print("BUGS!!!!!!")
end
[/CODE]
[QUOTE=Lavacoal123;50711518]Question about [URL="http://wiki.garrysmod.com/page/Global/IsValid"]IsValid[/URL]: if it returns a Boolean variable, then do you use it like this?
[CODE]
if IsValid(Weapon) == true then
--Hook stuff
else
print("BUGS!!!!!!")
end
[/CODE][/QUOTE]
Yep, but I prefer this:
[CODE]
if IsValid(Weapon) then
[/CODE]
Because lua doesn't need the == true, it checks that automatically.
[QUOTE=MPan1;50711583]Yep, but I prefer this:
[CODE]
if IsValid(Weapon) then
[/CODE]
Because lua doesn't need the == true, it checks that automatically.[/QUOTE]
Oh, right, because that's just like saying "if true then", thank you
I'll try the script now...
[editline]14th July 2016[/editline]
Well, there are no console errors (YAY!) but the SWEP doesn't work because once SWEP:Initialize is only ran once, and it just says what I programmed it to say when IsValid returns false. I should try to reload the SWEP actually. DANGIT! WHY DIDN'T I THINK OF THAT EARLIER! UGH! Well anyway, I'll get to that, but this is what it looks like now:
[CODE]
//JetPack SWEP Script by Lavacoal123
SWEP.DrawCrosshair = false
SWEP.Weight = 5
SWEP.ViewModel = "models/coal/jetpackstuff/jetpack_v.mdl"
SWEP.WorldModel = "models/weapons/w_fists.mdl"
SWEP.HoldType = "Pistol"
SWEP.ViewModelFOV = 64
SWEP.Slot = 0
SWEP.Purpose = "Shoot yourself high in the sky!"
SWEP.AutoSwitchTo = true
SWEP.Contact = "coalstudios95@gmail.com"
SWEP.Author = "Lavacoal123"
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
SWEP.SlotPos = 0
SWEP.Instructions = "Jump while equipped"
SWEP.base = "weapon_base"
SWEP.Category = "Coal's SWEPs"
SWEP.PrintName = "Basic Jetpack"
local Fuel = 35
local Used = 0
concommand.Add("jetfuel", function(ArgStr)
print("Your jetpack fuel is " .. Fuel .. " Out of 35!")
if ( ArgStr == "used" ) then
Used = 35 - Fuel
print("So far, you used " .. Used .. " Units of fuel.")
end
end )
function SWEP:Think()
--Sets current health to a very high number when equipped.
self.Owner:SetHealth(999999999999)
if ( Fuel == 0 ) then
hook.Remove( "KeyPress", "keypress_rocket" )
end
end
function SWEP:Reload()
print("Fully reloaded.")
hook.Add( "KeyPress", "keypress_rocket", function( ply, key )
if ( key == IN_JUMP ) then -- When the jump key is pressed...
ply:SetVelocity( ply:GetVelocity() + Vector( 0, 0, 10 ) ) --Set velocity to up 10 than it allready is
self.Weapon:EmitSound("jetpackfire.wav", 400, 400 ) -- Emit jetpack sound from weapon
Fuel = Fuel - 1
self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK) -- START FLAILING YOUR ARMS AROUND!!!
end
end )
end
function SWEP:Initialize()
if IsValid(Weapon) then
print("Mash the jump button to accelerate!") --On equip, tell user how to use.
hook.Add( "KeyPress", "keypress_rocket", function( ply, key )
if ( key == IN_JUMP ) then -- When the jump key is pressed...
ply:SetVelocity( ply:GetVelocity() + Vector( 0, 0, 10 ) ) --Set velocity to up 10 than it allready is
self.Weapon:EmitSound("jetpackfire.wav", 400, 400 ) -- Emit jetpack sound from weapon
Fuel = Fuel - 1
self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK) -- START FLAILING YOUR ARMS AROUND!!!
end
end )
else
print("Uh-Oh! Something happened wrong!")
end
end
[/CODE]
[editline]14th July 2016[/editline]
It works when I reload it! Except I think that there is too little velocity for too much fuel, so right now its more like a double jump tool, which is a simple fix, but all-in-all, it works!
Sorry, you need to Log In to post a reply to this thread.