Jump to content

Player damage script help


Recommended Posts

Hey guys, I quickly made up a little script to damage players in car accidents but the problem is it simply takes the vehicles damage of the player.

The main problem with this is, if the player has 1 health and then breaks a headlight whilst the car is barely moving, the player will still be killed.

I have a function(found it in my script, I think it came from the wiki) that can get the cars speed, but I am not entirely sure how to work it into this little script.

Script for car damage:

function vehicleLoss(loss) 
    local thePlayer = getVehicleOccupant(source) 
    if(thePlayer) then -- Check there is a player in the vehicle 
        if(getElementData(thePlayer,"kcar") ~= true) then 
            local th = getElementHealth(thePlayer)-tonumber(loss) 
            if(th <= 0) then 
                setElementHealth(thePlayer,0) 
            else 
                setElementHealth(thePlayer,th) 
            end 
        end 
    end 
end 
  
addEventHandler("onVehicleDamage", getRootElement(), vehicleLoss) 
  
function kCar(plr,cmd) 
    if(plr) then 
        if(getElementData(plr,"kcar") == true) then 
            setElementData(plr,"kcar","no") 
            outputChatBox("You will now die in car accidents",plr) 
        else 
            setElementData(plr,"kcar",true) 
            outputChatBox("You will no longer die in car accidents",plr) 
        end 
    end 
end 
addCommandHandler("kc",kCar) 
  

Little speed function I think I found on the wiki long ago:

function getPlayerSpeed(player,mph) 
  local velX,velY,velZ=getElementVelocity(getPedOccupiedVehicle(player) or player) 
  return (velX^2+velY^2+velZ^2)^0.5*(mph and 100 or 161) 
end 

Now, while I might be able to work that out if I try some more, I'd also like to take rolling into consideration, so even if the vehicle isn't going incredibly fast, rolling down a hill could still kill the driver of the vehicle.

Now, I am going to try some ideas if I can...right after I code the part that actually damages passengers(Don't worry, I know how to do this).

Now, onto something slightly less important;

I was playing on a server that added extra seats and such to the Nevada plane(probably a whole bunch of other vehicles too)

I'd like to do something similar and was wondering how people go about coding this custom seats?(glue the player inside and work a mouse-movable camera script or something?)

Link to comment

I tried this(one of many kinds of randomness) :

function vehicleDamage(theHitElement, force, bodypart, collisionX, collisionY, collisionZ, velocityX, velocityY, velocityZ, hitElementForce, model) 
    if(getElementData(player,"kcar") ~= true) then 
        local fDamageMultiplier = getVehicleHandling(source).collisionDamageMultiplier 
        local damage = force * fDamageMultiplier 
        local perc = (damage/1000) * 100 
        local player = getVehicleOccupant(source) 
        local newHealth = getElementHealth(player) - perc 
        setElementHealth(player,newHealth) 
    end 
end 
addEventHandler("onClientVehicleCollision", root, vehicleDamage) 

But I just die, even if I am stopped and reversing into a wall.

Plus I cannot figure out a good ratio for damage to player damage.

Any idea?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...