Hey there,
first things first: You don’t need comments like -- end if DoesEntityExist
if you indent your lines. It will be way more easy to read:
Citizen.CreateThread(function() --Creates thread
while true do
Citizen.Wait(1)
local aiming, targetPed = GetEntityPlayerIsFreeAimingAt(PlayerId(-1))
local id = GetPlayerName(PlayerId())
if aiming then
if DoesEntityExist(targetPed) and IsEntityAPed(targetPed) then
TriggerServerEvent('aimlogs:playeraim', id… ' aiming')
end – end if DoesEntityExist
end – end if aiming
end – end while
end)
So, as to the problems:
First off I’m not an experienced coder so whatever I post here might not actually be a good idea. If you think that, just post that.
It is going to print very quickly since you run this on a Wait(1)
which is pretty quick. Your code should be able to survive a Wait(50)
which would also be more performant friendly.
Now another option would be:
You want to display only once every time the player aims at another person. You could do it like this: Behind the line “TriggerServerEvent” add the following code:
while GetEntityPlayerIsFreeAimingAt(PlayerId(-1)) == targetPed do
Wait(200)
end
It could look like this:
Citizen.CreateThread(function() --Creates thread
while true do
Citizen.Wait(1)
local aiming, targetPed = GetEntityPlayerIsFreeAimingAt(PlayerId(-1))
local id = GetPlayerName(PlayerId())
if aiming then
if DoesEntityExist(targetPed) and IsEntityAPed(targetPed) then
TriggerServerEvent('aimlogs:playeraim', id… ' aiming')
while GetEntityPlayerIsFreeAimingAt(PlayerId(-1)) == targetPed do
Wait(200)
end
end – end if DoesEntityExist
end – end if aiming
end – end while
end)
As for the name of the player in scope:
Currently there is no code at all in order to print the target ped. Now you need to do a few other checks:
First is the native IsPedAPlayer(targetPed)
. You will not get a proper name if the entity you aim at is not a player. You can then choose: Do you want to print every single time your player is aiming at an NPC?
No matter the answer of this question, we need to add a check behind the checks whether the target is actually a ped:
if IsPedAPlayer(targetPed) then
-- get the players' name
TriggerServerEvent('aimlogs:playeraim', id… ' aiming')
while GetEntityPlayerIsFreeAimingAt(PlayerId(-1)) == targetPed do
Wait(200)
end
else
TriggerServerEvent('aimlogs:playeraim', id… ' aiming at an NPC.')
while GetEntityPlayerIsFreeAimingAt(PlayerId(-1)) == targetPed do
Wait(200)
end
end
Getting the players’ name will actually be a problem. You wanna use GetPlayerName()
native however you will need the Player ID for this.
You could try using the native NetworkGetPlayerIndexFromPed(targetPed)
to see if this returns you the player index. Otherwise I’d have to check whether I already did something like this.
If you got the playername, it will be easy to get it into the log-bot. Do it the same way as you already did for your name, using id ... 'aiming at ' ... target
.
Hope I didn’t write too much bullshit here