Jump to content

[RESOLVED] Is Ped Aim at the player


MrDante

Recommended Posts

First of all, I am Brazilian sorry for bad English :oops:

Hello Guys!, I have a problem(novelty :lol::lol::cry: ), finally, I wanted the player to shoot the ped and ped starts shooting at player, only has two problems, that ped not shoot the player, and that all peds that starts shooting, not only those selected,I looked everywhere in wiki and not found, anyone knows how to solve?

Client

function atirar(attacker) 
    givePedWeapon(source, 31, 5000, true) 
    setPedControlState(source, "aim_weapon", true) 
    setPedControlState(source, "fire", true) 
end 
addEventHandler("onClientPedDamage", root, atirar) 
addEvent("atirar", true) 
addEventHandler("atirar", root, atirar) 

Server

local ped1 = createPed ( 170, 291.88595581055, -108.11327362061, 1001.515625, 266.90405273438,true )  
local ped2 = createPed ( 215, 292.69845581055, -108.1056060791, 1001.515625, 88.905662536621,true )  
local vendedor1 = createPed ( 67, 290.14352416992, -111.51473999023, 1001.515625, 354.35003662109,true ) 
local vendedor2 = createPed ( 28, 291.11795043945, -104.48545837402, 1001.515625, 181.09841918945,true ) 
 -- The peds (ped1, ped2) They are shooting well, but I have not selected them to shoot 
  
function atiranovendedor(source) 
triggerClientEvent(source, "atirar", vendedor1) 
triggerClientEvent(source, "atirar", vendedor2) 
end 

Edited by Guest
Link to comment
What is atiranovendedor?

Use setPedTarget to let the ped shoot at the attacker.

atiranovendedor = shoot for seller

I tried this but not given

function atirar(attacker) 
    givePedWeapon(source, 31, 5000, true) 
    setPedControlState(source, "fire", true) 
    local x, y, z = getElementPosition(source) 
    setPedAimTarget(attacker, x, y, z) 
end 
addEventHandler("onClientPedDamage", root, atirar) 
addEvent("atirar", true) 
addEventHandler("atirar", root, atirar) 
  

or

function atirar(attacker) 
    givePedWeapon(source, 31, 5000, true) 
    setPedControlState(source, "fire", true) 
    local x, y, z = getElementPosition(localPlayer) 
    setPedAimTarget(source, x, y, z) 
end 
addEventHandler("onClientPedDamage", root, atirar) 
addEvent("atirar", true) 
addEventHandler("atirar", root, atirar) 
  

Link to comment

The setPedControlState might be cancelled due to your setPedAimTarget. Try to first set the target and then toggle the fire state. Also, you need to take the position of the 'attacker' and I'm not quite sure if you can handle this in the client side. I guess it would be better to do this on the server's side. Since you don't want a ped to hit 2 different players when 2 clients start shooting at them. Both of them will randomly die for others.

Link to comment

I did not understand at the change the state of the fire, and need setPedControlState because without it the ped does not shoot, and I have no idea how to catch the attacker's position on the server side and move to the client because the setPedAimTarget's side then the client would not in any way, and if I use that function that I showed in the previous post ped sights in the position of the player, but if the player moves, the ped the aim remains the same position and does not follow the player

First of all, I am Brazilian sorry for bad English :oops:

Link to comment
  • Moderators

Script 75% at serverside and 25% clientside. Serverside decides which player a ped should attack, not the client. The client should only give the server the required data, so that serverside can make the right decision.

The basic:

Client

- Send information to serverside. (is localplayer visible for the ped) (trigger)

Server

- Collect information. (in table)

- Process information and make a decision.

- Tell clients which effect. (trigger)

Client

- Show effect.

Link to comment
Script 75% at serverside and 25% clientside. Serverside decides which player a ped should attack, not the client. The client should only give the server the required data, so that serverside can make the right decision.

The basic:

Client

- Send information to serverside. (is localplayer visible for the ped) (trigger)

Server

- Collect information. (in table)

- Process information and make a decision.

- Tell clients which effect. (trigger)

Client

- Show effect.

Thanks for information, but I give an example with my script?

Link to comment
  • Moderators

I am not going to create large global examples for your code. You are the one that has to convert this information in to a script or at least trying to do so. And I am willing to help you with that, but you have to take those first steps.

Link to comment
I am not going to create large global examples for your code. You are the one that has to convert this information in to a script or at least trying to do so. And I am willing to help you with that, but you have to take those first steps.

Ok... I tried something like this, but continues in the same way

Server

function atiranovendedor() 
setPedControlState(vendedor1, "fire", true) 
setPedControlState(vendedor2, "fire", true) 
triggerClientEvent(source, "atirar", vendedor1) 
triggerClientEvent(source, "atirar", vendedor2) 
end 

Client

  
function atirar() 
if getElementData(source, "tiro") then 
    cancelEvent() -- let invincible 
    givePedWeapon(source, 31, 5000, true) 
    local x, y, z = getElementPosition(localPlayer) 
    setPedAimTarget(source, x, y, z) 
    end 
end 
addEventHandler("onClientPedDamage", root, atirar) 
addEvent("atirar", true) 
addEventHandler("atirar", root, atirar) 

Link to comment
  • Moderators

First start with detecting if a ped should attack the localplayer.

When to detect?

[url=https://wiki.multitheftauto.com/wiki/OnClientRender]https://wiki.multitheftauto.com/wiki/OnClientRender[/url]  

How to detect? (simple version)

  
[url=https://wiki.multitheftauto.com/wiki/GetElementPosition]https://wiki.multitheftauto.com/wiki/GetElementPosition[/url] > player 
[url=https://wiki.multitheftauto.com/wiki/GetElementsByType]https://wiki.multitheftauto.com/wiki/GetElementsByType[/url] > ped 
-- loop 
[url=https://wiki.multitheftauto.com/wiki/GetElementPosition]https://wiki.multitheftauto.com/wiki/GetElementPosition[/url] > ped 
[url=https://wiki.multitheftauto.com/wiki/GetDistanceBetweenPoints3D]https://wiki.multitheftauto.com/wiki/Ge ... enPoints3D[/url] > compare distance between player and ped 

Link to comment
First start with detecting if a ped should attack the localplayer.

When to detect?

[url=https://wiki.multitheftauto.com/wiki/OnClientRender]https://wiki.multitheftauto.com/wiki/OnClientRender[/url]  

How to detect? (simple version)

  
[url=https://wiki.multitheftauto.com/wiki/GetElementPosition]https://wiki.multitheftauto.com/wiki/GetElementPosition[/url] > player 
[url=https://wiki.multitheftauto.com/wiki/GetElementsByType]https://wiki.multitheftauto.com/wiki/GetElementsByType[/url] > ped 
-- loop 
[url=https://wiki.multitheftauto.com/wiki/GetElementPosition]https://wiki.multitheftauto.com/wiki/GetElementPosition[/url] > ped 
[url=https://wiki.multitheftauto.com/wiki/GetDistanceBetweenPoints3D]https://wiki.multitheftauto.com/wiki/Ge ... enPoints3D[/url] > compare distance between player and ped 

I have not tested now, but is that correct?

Client

function atirar() 
if getElementData(source, "tiro") then 
cancelEvent() 
local ped = getElementsByType ( "ped" ) 
for theKey, thePed in ipairs(ped) do     
    local x ,y, z = getElementPosition(thePed) 
    local sx, sy, sz = getElementPosition(localPlayer) 
    local Dist = getDistanceBetweenPoints3D (x, y, z, sx, sy, sz) 
    givePedWeapon(thePed, 31, 5000, true) 
    setPedAimTarget(thePed, sx, sy, sz) 
    end 
end 
addEventHandler("onClientPedDamage", root, atirar) 
addEvent("atirar", true) 
addEventHandler("atirar", root, atirar) 

Server

function atiranovendedor() 
local ped = getElementsByType ( "ped" ) 
    for theKey, thePeds in ipairs (ped) do                   
triggerClientEvent(source, "atirar", thePeds) 
end 
end 
end 

I never got to use loop, always had doubts with him

Link to comment

Client

function onClientRender() 
local ped = getElementsByType ( "ped" ) 
    for theKey, thePed in ipairs(ped) do  
        if (isElement(thePed)) then 
        local x ,y, z = getElementPosition(thePed) 
            local sx, sy, sz = getElementPosition(localPlayer) 
            local Dist = getDistanceBetweenPoints3D (x, y, z, sx, sy, sz) 
            givePedWeapon(thePed, 31, 5000, true) 
            setPedControlState(thePed, "right", false) 
            setPedControlState(thePed, "fire", true) 
            setTimer(function() setPedControlState(thePed, "right", true) end, 3000, 1) 
            setTimer(function() setPedControlState(thePed, "fire", true) end, 1000, 1) 
        if (Dist < 2) then 
            setPedAimTarget(thePed, sx, sy, sz) 
                elseif (Dist > 4 ) then 
                    setPedAimTarget(thePed, sx, sy, sz) 
                elseif (Dist > 5 ) then 
                    setPedAimTarget(thePed, sx, sy, sz) 
                elseif(Dist > 6) then 
                    setPedAimTarget(thePed, sx, sy, sz) 
                elseif(Dist > 7) then 
                    setPedAimTarget(thePed, sx, sy, sz) 
                elseif(Dist > -- s8) --> then 
                    setPedAimTarget(thePed, sx, sy, sz) 
                elseif(Dist > 9) then 
                setPedAimTarget(thePed, sx, sy, sz) 
                elseif(Dist > 10) then 
                setPedControlState(thePed, "fire", false) 
                setPedControlState(thePed, "right", false) 
            end 
        end 
    end 
end 
  
  
function peddamage(thePed) 
    if (isElement(thePed)) then 
        if getElementData(thePed,"tiro") then 
            cancelEvent()    
            addEventHandler("onClientRender", root, onClientRender)  
            onClientRender() 
        end 
    end 
end 
addEventHandler("onClientPedDamage", root, peddamage) 
addEvent("fire", true) 
addEventHandler("fire", root, peddamage) 

Server

function atiranoped() 
triggerClientEvent(source,"fire", root) 
  
end 

I do not know where to move the onClientRender the function '-'

Link to comment
What the f.u.c.k. are you doing? How hard is it to follow a few instructions and letting go from your old code?

My God !, I went in his direction and tried man, I'm sorry if I have difficulty :|

ok, I did without my old functions, and now has an Error?

Client

function peddamage() 
        end 
addEvent("fire", true) 
addEventHandler("fire", root, peddamage) 
addEventHandler("onClientRender", root, peddamage) 

Server

function atiranoped() 
for index, player in ipairs(getElementsByType("ped")) do 
    triggerClientEvent(source,"fire", root) 
    end 
end 

Link to comment
  • Moderators

This is as far I go, study the code well and do you benefit with it. It is not tested, but this is one of the better ways to make it.

local thisResourceDynamicRoot = getResourceDynamicElementRoot(getThisResource()) 
local spottedData = {} -- prevent data overuse. 
  
  
addEventHandler("onClientRender", root, 
function () 
    local playerX,  playerY, playerZ = getElementPosition(localPlayer) 
    local peds = getElementsByType("ped", thisResourceDynamicRoot, true) 
     
    local pedsSpottedPlayer = {} 
     
    for i=1,#peds do  
        local ped = peds[i] 
        local pedX, pedY, pedZ = getElementPosition(ped) 
        if not spottedData[ped] and getDistanceBetweenPoints3D(playerX,  playerY, playerZ, pedX, pedY, pedZ) < 20 then  
            spottedData[ped] = true 
            pedsSpottedPlayer[#pedsSpottedPlayer+1] = ped 
        end 
    end 
    if #pedsSpottedPlayer > 0 then 
        triggerServerEvent("spottedByPed",localPlayer,pedsSpottedPlayer,true) 
    end 
end) 
  
addEventHandler("onClientElementStreamOut",thisResourceDynamicRoot, 
function () 
    if spottedData[source] then  
        spottedData[source] = nil 
        triggerServerEvent("spottedByPed",localPlayer,{ped},false) 
    end 
end) 
  

addEvent("spottedByPed",true) 
addEventHandler("spottedByPed",root, 
function (peds, spottedStatus) 
    if client == source and isElement(source) then -- validate. 
        --local player = source 
    end 
end) 

Link to comment

Thank you, but I managed to do in a way, nothing complex

OBS: I had no need to use the server, but only to define the data element

Client:

  
function onPedRender() 
local ped = getElementsByType ( "ped" ) 
    for theKey, thePed in ipairs(ped) do  
        if getElementData(thePed, "shoot") then 
            cancelEvent() 
            local x ,y, z = getElementPosition(thePed) 
            local sx, sy, sz = getElementPosition(localPlayer) 
            local Dist = getDistanceBetweenPoints3D (x, y, z, sx, sy, sz) 
            givePedWeapon(thePed, 31, 5000, true) 
            setPedControlState(thePed, "right", false) 
            setPedControlState(thePed, "fire", true) 
            setPedAimTarget(thePed, sx, sy, sz) 
            addEventHandler("onClientRender", root, onPedRender) 
            end 
        end 
    end 
addEventHandler("onClientPedDamage", root, onPedRender) 

Server

  
local vendedor1 = createPed ( 67, 290.14352416992, -111.51473999023, 1001.515625, 354.35003662109,true ) 
local vendedor2 = createPed ( 28, 291.11795043945, -104.48545837402, 1001.515625, 181.09841918945,true) 
setElementData(vendedor1, "shoot", true) 
setElementData(vendedor2, "shoot", true) 
  

Thank you for the patience

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