GetPlayerWantedLevel does not work

Hey,

My goal is when the player leaves the server if he has more than one star not save the player in the database.

The problem is that this piece of code “GetPlayerWantedLevel(xPlayer)” always returns wanted level to 0.

Sorry for my english and thank you.

Is xPlayer the player’s ID? I assume not just by looking at the rest of the code.

This piece of code is part of the es_extended function.lua script xPlayer returns table: 0000023C292C2AD0

use xPlayer.source

does GetPlayerWantedLevel(PlayerId()) also give you 0 ?

xPlayer.source works thanks

You’re welcome, don’t forget to set as resolved

is there any way to save wanted level when user leave the server ?
is there any way to save wanted level in DB ???
thank you

Yes of course, you can pass the wanted level as an argument while triggering a server event who saves it in the database

When the player leaves the server, you cannot save or fetch their wanted level as it’s too late - they’ve disconnected.

The solution to your scenario is to store the player’s wanted level server-side by triggering a server event the moment they receive the wanted level. If the player’s wanted level increases/decreases then call your server to update itself again so your server is aware of the current wanted level for your player all the time. Then when the player disconnects, you can simply use the disconnected player’s ID as a reference to fetch their wanted level from the server to store on the database.

thanks for your replay
i understand what you said but cause i am new on developing i will be hard for me to do it

but i can try ifd you help me and send me example
thank you very much

Of course, I’ll give you a rough example:

When player first receives a wanted level, we can send that information from a client script to the server

local wanted_level = 3 -- let's assume this means 3 stars
TriggerServerEvent("server_store_wanted_level", wanted_level) -- we send the info to the server

Then on the server we have the following

local wanted_levels = {} -- global variable on the server side to hold all wanted levels
RegisterNetEvent("server_store_wanted_level")
AddEventHandler("server_store_wanted_level", function(wanted_level)
    local new_record = {} -- creating a new record
    new_record.player = source -- assigning the player who received the wanted level to our new record
    new_record.wanted_level = wanted_level -- storing the information we received from the client
    table.insert(wanted_levels, new_record)
end)

Now when a player drops, all we have to do is match the player’s ID to the player IDs we have stored on the server (if it exists). If we find a match, we save it to database, otherwise we ignore and don’t do anything.

Just a friendly side note, there are many different ways to fetch, store and save data from a player, I have just given a basic example so you’re able to better understand the concept :slight_smile:

iam really happy to see good person like you thank you very much i will try it and let you know :heart_eyes:

1 Like

wanted-blip.rar (2.4 KB)

i have this small script … it’s look like a blip , if any body enter the blib a get out he will get wanted level 5 stars
and also i add wanted level to database , then i upgrade the script with your example but it still working good but without saving the wanted level
thank you for your help and your time