• [Please Help] Broken Vehicle Damage Script
    14 replies, posted
I have a lua script for vehicle damage but when I run it I get an error talking about expecting a value but being a nil value. It says this either for the timer or for the dmginfo. [CODE]DefaultVehicleHP = 250 DeafultCleanupTime = 180 hook.Add("EntityTakeDamage", "VehicleHPHook", function:( ent, inflictor, attacker, amount, dmginfo ) if not ent:IsVehicle() then return end if not ent.HP then ent.HP = DefaultVehicleHP end ent.HP = ent.HP - dmginfo:GetDamage() if ent.HP <= 0 and not ent.Destroyed then ent.Destroyed = true ent:SetColor(0,0,0,255) local ExplodeTime = math.random(5,30) ent:Ignite(ExplodeTime,100) --[[if ent:GetDriver():IsPlayer() then ent:GetDriver():ExitVehicle() end if ent:GetPassenger():IsPlayer() then ent:GetPassenger():ExitVehicle() end --]] timer.Simple(ExplodeTime, function() if not ent then return end local explosion = ents.Create("env_explosion") explosion:SetPos(ent:GetPos()) explosion:SetOwner(attacker) explosion:Spawn() explosion:SetKeyValue("iMagnitude", "1000") explosion:Fire("Explode", 0, 0) if ent:GetDriver():IsPlayer() then ent:GetDriver():Kill() end if ent:GetPassenger():IsPlayer() then ent:GetPassenger():Kill() end if not ent then return end ent:Remove() end) end end) [/CODE] Any help is much appreciated.
[url]http://wiki.garrysmod.com/page/GM/EntityTakeDamage[/url] The EntityTakeDamage hooks has only 2 parameter. 1: Entity 2: CTakeDamageInfo You can determine the attacker by looking at the wiki page for CTakeDamageInfo.
[QUOTE=freakyy;45648415][url]http://wiki.garrysmod.com/page/GM/EntityTakeDamage[/url] The EntityTakeDamage hooks has only 2 parameter. 1: Entity 2: CTakeDamageInfo You can determine the attacker by looking at the wiki page for CTakeDamageInfo.[/QUOTE] I'm sorry I have no idea what you mean, I don't understand LUA I got this outdated script from somewhere. Can you explain this a different way or something? Thanks
He means that where you have (ent,inflictor,attacker,amount,dmginfo), only (ent,dmginfo) are valid (IN THAT ORDER) So, to create the other values, you'll have to define them like so: [code]local attacker = dmginfo:GetAttacker() local amount = dmginfo:GetDamage() local inflictor = dmginfo:GetInflictor()[/code]
Okay so I think I did it right but it isn't doing anything and the console is saying nothing. [CODE]DefaultVehicleHP = 3 DefaultCleanupTime = 180 hook.Add:("EntityTakeDamage", "VehicleHPHook", function:( ent,dmginfo ) local attacker = dmginfo:GetAttacker() local amount = dmginfo:GetDamage() local inflictor = dmginfo:GetInflictor() if not ent:IsVehicle() then return end if not ent.HP then ent.HP = DefaultVehicleHP end ent.HP = ent.HP - amount:GetDamage() if ent.HP <= 0 and not ent.Destroyed then ent.Destroyed = true ent:SetColor(0,0,0,255) local ExplodeTime = math.random(5,30) ent:Ignite(ExplodeTime,100) --[[if ent:GetDriver():IsPlayer() then ent:GetDriver():ExitVehicle() end if ent:GetPassenger():IsPlayer() then ent:GetPassenger():ExitVehicle() end --]] timer.Simple(ExplodeTime, function() if not ent then return end local explosion = ents.Create("env_explosion") explosion:SetPos(ent:GetPos()) explosion:SetOwner(attacker) explosion:Spawn() explosion:SetKeyValue("iMagnitude", "1000") explosion:Fire("Explode", 0, 0) if ent:GetDriver():IsPlayer() then ent:GetDriver():Kill() end if ent:GetPassenger():IsPlayer() then ent:GetPassenger():Kill() end if not ent then return end ent:Remove() end) end end) [/CODE]
I might be stupid but don't you need a space after your comma?
[QUOTE=fluidq;45650556]Okay so I think I did it right but it isn't doing anything and the console is saying nothing. [/QUOTE] Forget what was here before, I just realized you put a ":" between hook.Add and the arguments. Remove the ":" [QUOTE=JasonMan34;45650586]I might be stupid but don't you need a space after your comma?[/QUOTE] It's not compulsory.
I removed the :, added the local stuff. Nothing happens still.
I'll be honest, even I'm stuck by now.
[code]amount:GetDamage()[/code] It's doing something alright - errors. Get rid of :GetDamage() on that.
I removed the getdamage thing... Nothing still. I'll post what it looks like right now. [CODE]DefaultVehicleHP = 3 DefaultCleanupTime = 180 hook.Add("EntityTakeDamage", "VehicleHPHook", function:( ent,dmginfo ) local attacker = dmginfo:GetAttacker() local amount = dmginfo:GetDamage() local inflictor = dmginfo:GetInflictor() if not ent:IsVehicle() then return end if not ent.HP then ent.HP = DefaultVehicleHP end ent.HP = ent.HP - amount if ent.HP <= 0 and not ent.Destroyed then ent.Destroyed = true ent:SetColor(0,0,0,255) local ExplodeTime = math.random(5,30) ent:Ignite(ExplodeTime,100) --[[if ent:GetDriver():IsPlayer() then ent:GetDriver():ExitVehicle() end if ent:GetPassenger():IsPlayer() then ent:GetPassenger():ExitVehicle() end --]] timer.Simple(ExplodeTime, function() if not ent then return end local explosion = ents.Create("env_explosion") explosion:SetPos(ent:GetPos()) explosion:SetOwner(attacker) explosion:Spawn() explosion:SetKeyValue("iMagnitude", "1000") explosion:Fire("Explode", 0, 0) if ent:GetDriver():IsPlayer() then ent:GetDriver():Kill() end if ent:GetPassenger():IsPlayer() then ent:GetPassenger():Kill() end if not ent then return end ent:Remove() end) end end)[/CODE]
[code] hook.Add("EntityTakeDamage", "VehicleHPHook", function:( ent,dmginfo ) [/code] should be [code] hook.Add("EntityTakeDamage", "VehicleHPHook", function( ent,dmginfo ) [/code]
Okay I did what smithy said and now I'm genuinely stuck because I'm getting nothing. No errors in the console nothing. My RP server needs this but I just can't get it to function. It seems a pretty simple but outdated script. Would anyone be able to provide a gmod 13 version of this or something? I'm genuinely stuck for ideas :c EDIT: Holy shit! Something Happened! I spawned a sanic and I know then do a tonne of damage. When I drive my car in, I died! I also got a console response! Attempt to index local 'amount' (a number value) But why the hell does it take so much to damage it? I set the vehicle hp to 3...
[QUOTE=fluidq;45652005]Okay I did what smithy said and now I'm genuinely stuck because I'm getting nothing. No errors in the console nothing. My RP server needs this but I just can't get it to function. It seems a pretty simple but outdated script. Would anyone be able to provide a gmod 13 version of this or something? I'm genuinely stuck for ideas :c EDIT: Holy shit! Something Happened! I spawned a sanic and I know then do a tonne of damage. When I drive my car in, I died! I also got a console response! Attempt to index local 'amount' (a number value) But why the hell does it take so much to damage it? I set the vehicle hp to 3...[/QUOTE] A quick Google brought up this: [code] --[[==================================================================================================== ===SIMPLE VEHICLE HEALTH SCRIPT=== A simple script by Sven Brimstone (SonofBrim) based off of the similar 2010 script by SinTwins. Gives cars a set amount of health, sets them on fire and blows them up when that health is depleted. Comes with some customization. ====================================================================================================--]] -- Config Options defaulthp = 1800 -- Vehicle health smokepoint = 300 -- Point at which vehicles begin to smoke timemin = 5 -- Minimum time between the vehicle's health reaching 0 and detonation timemax = 20 -- Maximum time between the vehicle's health reaching 0 and detonation smokeeffect = "smoke_burning_engine_01" -- Vehicle smoke effect explodeeffect = "explosion_huge_f" -- Explosion effect -- Give cars health when they spawn. hook.Add("PlayerSpawnedVehicle", "VehicleHP", function( ply, ent ) if not ent:IsValid() then return end ent:SetHealth( defaulthp ) ent.Screwed = false -- More or less lets later parts of the code know whether the car needs to be blow up or not. ent.Hurt = false end) -- Make cars take damage and blow up if they are damaged too much. hook.Add("EntityTakeDamage", "VehicleHPHook", function( ent, dmginfo ) if not ent:IsVehicle() then return end local damage = dmginfo:GetDamage() -- Compensate for weird vehicle damage system, make explosives do more damage. if dmginfo:IsBulletDamage() then ent:SetHealth(ent:Health() - (10000 * damage)) elseif dmginfo:IsExplosionDamage() then ent:SetHealth(ent:Health() - (8 * damage)) else ent:SetHealth(ent:Health() - damage) end -- This was used for testing purposes, spits out a bunch of damage info to the console. -- print(ent:GetAttachments(), dmginfo:GetDamageType(), dmginfo:GetAmmoType(), damage, dmginfo:IsBulletDamage()) -- Make the vehicle smoke if health gets low. if ent:IsValid() and ent:Health() <= smokepoint and ent.Hurt == false then local id = ent:LookupAttachment("vehicle_engine") ent.Hurt = true -- Let it know that the car is hurt so it doesn't add the effect the next time it gets damaged. ParticleEffectAttach( smokeeffect, PATTACH_POINT_FOLLOW, ent, id ) end -- Once health drops to 0 or below, light the car on fire, kick everyone out, and blow it up. if ent:IsValid() and ent:Health() <= 0 and ent.Screwed == false then ent.Screwed = true -- Let it know that the car is going to blow up and the timer is started (so it doesn't keep on restarting) local randomtime = math.random(timemin,timemax) ent:Ignite( randomtime, 100 ) -- Boot the driver out if ent:GetDriver():IsValid() then ent:GetDriver():ExitVehicle(ent) end -- Blow it up after a set time. timer.Create("CarExplode", randomtime, 1, function() if not ent:IsValid() then return end local explosion = ents.Create("env_explosion") ParticleEffect(explodeeffect,ent:GetPos(),Angle(0,0,0),nil) explosion:SetPos(ent:GetPos()) explosion:SetOwner(attacker) explosion:Spawn() explosion:SetKeyValue("iMagnitude", "250") explosion:Fire("Explode", 0, 0) ent:Remove() end) end end) -- If a car is going to blow up, don't let players in. hook.Add("CanPlayerEnterVehicle", "ExplodedVehicles", function(ply, ent, role) if ent.Screwed then return false end end) [/code] Looks like it should work, however I cannot test myself right now
I never saw that! Thankyou soooo much! I'll test it now :D EDIT: Great, shooting the car works and causes the desired effect however crashing the car at high speeds does nothing. Is there a way of making that happen?
Sorry, you need to Log In to post a reply to this thread.