Jump to content

Clientside damage.


GERgta

Recommended Posts

Hello forums,

I've made a script that makes all the damage clientside and multiplies it by 2. But something doesn't seem to work (player still recieves normal damage). The XML file is 100% okay, I think its the script.

Can you guys help me?

function damage ( attacker, weapon, bodypart, loss ) 
    if attacker == localPlayer then 
        local playerHealth = getElementHealth ( source ) 
        local damageMult = loss * 2 
        local newHealth = playerHealth - damageMult 
        if newHealth > 0 then 
            cancelEvent() 
            setElementHealth ( source, newHealth ) 
        else 
            cancelEvent() 
            setElementHealth ( source, 0 ) 
        end 
    end 
end 
addEventHandler ( "onClientPlayerDamage", getRootElement(), damage ) 

Link to comment

if attacker == localPlayer then 

Therefore the rest of the function is only executed if local player is the one who inflicted damage. And since client-side scripts can only control the health of local player, this results in script actually taking effect if player causes damage to himself/herself.

Link to comment

Therefore the rest of the function is only executed if local player is the one who inflicted damage.

I had to make it like this, cause else its not really clientside.

And since client-side scripts can only control the health of local player, this results in script actually taking effect if player causes damage to himself/herself.

Oh man, I forgot about it... Well that's unfortunate. I also tried to connect this with some serverside events (triggerServerEvent), but it didn't seem to work right (Event not found?). I could make this completely serverside, but then it wouldn't be a clientside hit-detection. Is there any solution for this?

Link to comment
Did you use
addEvent 
addEventHandler 

I will try this now, thanks for the fast reply.

EDIT: Oh my god, thank you. Everything seems to work now!

Source:

You are allowed to claim it as yours, etc, etc. But don't sell it, not this little piece of coding! Have fun!

Client.lua:

function damage ( attacker, weapon, bodypart, loss ) 
    if attacker == localPlayer then 
        local playerHealth = getElementHealth ( source ) 
        local damageMult = loss * 2 
        local newHealth = playerHealth - damageMult 
        triggerServerEvent ( "damage", source, attacker, weapon, bodypart, loss, newHealth ) 
        cancelEvent() 
    end 
end 
addEventHandler ( "onClientPlayerDamage", getRootElement(), damage ) 
  
function pedDamage ( attacker, weapon, bodypart, loss ) 
    if attacker == localPlayer then 
        local playerHealth = getElementHealth ( source ) 
        local normalDamage = playerHealth - loss 
        if bodypart == 9 and normalDamage > 0 then 
            setElementHealth ( source, 1 ) 
        else 
            if normalDamage > 0 then 
                local damageMult = loss * 10 
                local newHealth = playerHealth - damageMult 
                triggerServerEvent ( "damage", source, attacker, weapon, bodypart, loss, newHealth ) 
                cancelEvent() 
            end 
        end 
    end 
end 

Server.lua:

addEvent ( "damage", true ) 
  
function damage ( attacker, weapon, bodypart, loss, newHealth ) 
    if newHealth > 0 then 
        setElementHealth ( source, newHealth ) 
    else 
        killPed ( source, attacker, weapon, bodypart ) 
    end 
end 
addEventHandler ( "damage", getRootElement(), damage ) 

Edited by Guest
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...