Giving something to an offline user

Ако имате затруднения при изработката/преработката на даден плъгин - пишете тук, ще се опитаме да ви помогнем!
Отговори
Потребителски аватар

Автор на темата
X3.!
Foreigner
Foreigner
Мнения: 14
Регистриран: 30 ное 2018, 20:46

Giving something to an offline user

Мнение от X3.! » 04 дек 2018, 22:04

Hi
As the title says, how I can give something to an offline user? by steamid.. for example, XP.

Потребителски аватар

OciXCrom TM
Администратор
Администратор
Мнения: 4601
Регистриран: 06 окт 2016, 19:20
Местоположение: /resetscore
Се отблагодари: 38 пъти
Получена благодарност: 375 пъти
Години: 21
Контакти:

Giving something to an offline user

Мнение от OciXCrom TM » 05 дек 2018, 14:29

It depends on how you save your data. Which saving method are you using? nVault?

Потребителски аватар

Автор на темата
X3.!
Foreigner
Foreigner
Мнения: 14
Регистриран: 30 ное 2018, 20:46

Giving something to an offline user

Мнение от X3.! » 05 дек 2018, 23:00

Yes, im using nVault

Потребителски аватар

OciXCrom TM
Администратор
Администратор
Мнения: 4601
Регистриран: 06 окт 2016, 19:20
Местоположение: /resetscore
Се отблагодари: 38 пъти
Получена благодарност: 375 пъти
Години: 21
Контакти:

Giving something to an offline user

Мнение от OciXCrom TM » 06 дек 2018, 16:11

If you want to do it by SteamID, you must save the data by SteamID as well because you'll need to locate the SteamID in the nVault file that corresponds with the player.

To give XP to the player, you'll simple need to load the current XP he has and add the new amount to that number. Here's a simple example on how to give 100 XP to the player with SteamID STEAM_0:0:50153248, step by step:

Код: Избери всички

// Get the current XP from the SteamID
new iXP = nvault_get(g_iVault, "STEAM_0:0:50153248")

// Add 100 to the current XP
iXP += 100

// Create a string that will hold the XP because nVault data can't be saved as a number, it must be a string
new szXP[32]
num_to_str(iXP, szXP, charsmax(szXP))

// Save the data to nVault
nvault_set(g_iVault, "STEAM_0:0:50153248", szXP)
The same thing can be done in just one line of code if you're using AMXX 1.8.3 or a newer version:

Код: Избери всички

nvault_set(g_iVault, "STEAM_0:0:50153248", fmt("%i", nvault_get(g_iVault, "STEAM_0:0:50153248") + 100))

Потребителски аватар

Автор на темата
X3.!
Foreigner
Foreigner
Мнения: 14
Регистриран: 30 ное 2018, 20:46

Giving something to an offline user

Мнение от X3.! » 06 дек 2018, 23:20

So is that correct?

Код: Избери всички

public CmdAddXPOff(iPlayer, level, cid)
{
	if(!cmd_access(iPlayer,level,cid,3)) return PLUGIN_HANDLED
	
	new szAuthID[35]
	get_user_authid(iPlayer,szAuthID,charsmax(szAuthID))
	
	read_argv(1,szAuthID,charsmax(szAuthID))
	
	new XP[32]
	read_argv(2,XP,charsmax(XP))
	
	new xpgiven = str_to_num(XP)
	
	new target[32]
	new player = cmd_target(iPlayer,target, CMDTARGET_OBEY_IMMUNITY | CMDTARGET_NO_BOTS | CMDTARGET_ALLOW_SELF)
	
	if(target[iPlayer] == szAuthID[iPlayer])
	{
		new iXP = nvault_get(g_Vault,szAuthID)
		
		iXP += xpgiven
		
		new szXP[32]
		num_to_str(iXP,szXP,charsmax(szXP))
		
		nvault_set(g_Vault,szAuthID,szXP)
	}
}

Потребителски аватар

OciXCrom TM
Администратор
Администратор
Мнения: 4601
Регистриран: 06 окт 2016, 19:20
Местоположение: /resetscore
Се отблагодари: 38 пъти
Получена благодарност: 375 пъти
Години: 21
Контакти:

Giving something to an offline user

Мнение от OciXCrom TM » 07 дек 2018, 15:53

No, why are you trying to target a player if he is offline? cmd_target only works for online players. The SteamID is only a key stored in the nVault file, it's not an actual player that you can locate.

Also, you're getting the SteamID of the player who used the command for no reason.

This line also isn't correct:

Код: Избери всички

if(target[iPlayer] == szAuthID[iPlayer])
I don't know what you're trying to check here.

Here's what the code should look like:

Код: Избери всички

// amx_addxp <steamid> <xp>
public CmdAddXPOff(id, iLevel, iCid)
{
	// Check if the player has access and both arguments (<steamid> and <xp>) are provided
	if(!cmd_access(id, iLevel, iCid, 3))
		return PLUGIN_HANDLED
	
	// Grab the first argument which is <steamid>
	new szAuthID[35]
	read_argv(1, szAuthID, charsmax(szAuthID))
	
	// Grab the second argument which is <xp>
	new szXP[32]
	read_argv(2, szXP, charsmax(szXP))

	// Convert the argument to an integer
	new iXP = str_to_num(szXP)

	// Grab the current XP for the SteamID, add the XP from the command and store it in a string
	num_to_str(nvault_get(g_Vault,szAuthID) + iXP, szXP, charsmax(szXP))

	// Write the data in the nVault file
	nvault_set(g_Vault, szAuthID, szXP)

	// Print a message to the admin
	console_print(id, "* Gave %d XP to %s", iXP, szAuthID)
	
	return PLUGIN_HANDLED
}
You should also add a safety check to see if the player with that SteamID is connected or not.

Потребителски аватар

Автор на темата
X3.!
Foreigner
Foreigner
Мнения: 14
Регистриран: 30 ное 2018, 20:46

Giving something to an offline user

Мнение от X3.! » 07 дек 2018, 18:11

Thanks, I ll try it

I was confused to check the target, thought that the cmd_target will work xd ( the first argument )

Отговори

Върни се в “Помощ в скриптирането”

Кой е на линия

Потребители, разглеждащи този форум: Няма регистрирани потребители и 1 гост