Jump to content

Recommended Posts

Hello community, could you please advise me on how to resolve the delay in determining player deaths? Specifically, when I shoot someone and they shoot me simultaneously, there are instances where both of us end up dying. I want to ensure that only one player dies in such situations.

Link to comment
Posted (edited)
30 minutes ago, Flashmyname said:

Can you provide more information on that "situation" that you mean? or some code snippet that will help us define the problem that you having?

 

I mean when we both shoot each other, we both die. I want only one to die

 

Example: https://imgur.com/a/1ajcl51 'The bug screenshot'

Edited by Snakegold
Link to comment
  • Moderators
7 hours ago, Snakegold said:

I want to ensure that only one player dies in such situations.

Technically it is possible, but it will only work as intended when the latency is very low (< 60 ping) and stable internet (no packet loss).

How it technically works is that the server is used as mediator for all the damage. When a player it's health is 0, all future damage done by this player is ignored.

But keep in mind that this will look very weird when the latency is too high. (unplayable)

 

Some basics:

Clientside

onClientPlayerDamage

cancelEvent

triggerServerEvent

 

Serverside

addEvent

addEventHandler

getElementHealth

setElementHealth

killPed
 

 

 

Link to comment

server side: 
 

function handlePlayerDamage(attacker, weapon, bodypart, loss)
    local victim = source

    if getElementHealth(victim) > 0 then
        setElementHealth(victim, getElementHealth(victim) - loss)
        
        if getElementHealth(victim) <= 0 then
            setElementData(victim, "isDead", true)
            
            killPed(victim)
        end
                cancelEvent()
    end
end
addEvent("onPlayerDamage", true)
addEventHandler("onPlayerDamage", root, handlePlayerDamage)

function isPlayerDead(player)
    return getElementData(player, "isDead") or false
end

client side:
 

function onClientPlayerDamage(attacker, weapon, bodypart, loss)
    triggerServerEvent("onPlayerDamage", localPlayer, attacker, weapon, bodypart, loss)
end
addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage)

DEBUG:
ERROR: Client "playername" triggered serverside event onPlayerDamage, but event is not marked as remotely triggerable

Link to comment
  • Moderators
On 11/03/2024 at 01:14, Snakegold said:

ERROR: Client "playername" triggered serverside event onPlayerDamage, but event is not marked as remotely triggerable

  • Double check if the serverside file is running and init as serverside.
  • Do not enable remote for native events. This is a security risk.
  • Use the predefined client variable for remote triggers, else cheaters can kill a whole server instantly.

 

iprint(triggerClientEvent and "serverside" or "clientside") -- inspect if the file is running serverside

addEvent("onPlayerDamage", false) -- disable remote

function handlePlayerDamage(attacker, weapon, bodypart, loss)
    if client ~= source then return end
    local victim = client
    triggerEvent("onPlayerDamage", victim, attacker, weapon, bodypart, loss) -- emulate the (client) cancelled onPlayerDamage event
    if getElementHealth(victim) > 0 then
        setElementHealth(victim, getElementHealth(victim) - loss)
        
        if getElementHealth(victim) <= 0 then
            setElementData(victim, "isDead", true)
            killPed(victim)
        end  
    end
end
addEvent("onCustomPlayerDamage", true)
addEventHandler("onCustomPlayerDamage", root, handlePlayerDamage)

function isPlayerDead(player)
    return getElementData(player, "isDead") or false
end

 

function onClientPlayerDamage(attacker, weapon, bodypart, loss)
    if attacker and getElementType(attacker) == "player" then
        triggerServerEvent("onCustomPlayerDamage", localPlayer, attacker, weapon, bodypart, loss)
        cancelEvent()
    end
end
addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage)

 

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...