SA-MP Forums Archive
I can't get this to work.. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: I can't get this to work.. (/showthread.php?tid=624017)



I can't get this to work.. - danielpalade - 09.12.2016

I have this small drug system which has the command /selldrugs. Everything is stored in an enum, here is the code:

Код:
enum drugs
{
	drugSender,
	drugReceiver,
	drugPrice,
	drugAmount
}
new SellDrugs[1000][drugs];

hook OnGamemodeInit()
{
	for(new i = 0; i < 1000; i++)
	{
		SellDrugs[i][drugSender] = INVALID_PLAYER_ID;
		SellDrugs[i][drugReceiver] = INVALID_PLAYER_ID;
		SellDrugs[i][drugPrice] = 0;
		SellDrugs[i][drugAmount] = 0;
	}
}

stock FreeDrugsSlot()
{
	for(new i=0; i < 1000; i++)
	{
		if(SellDrugs[i][drugSender] == -1) return i;
		break;
	}
	return -1;
}

stock HasSentDrugsTo(playerid, target)
{
	for(new i=0; i < 1000; i++)
	{
		if(SellDrugs[i][drugReceiver] == target && SellDrugs[i][drugSender] == playerid) return 1;
		break;
	}
	return 0;
}

CMD:selldrugs(playerid, params[])
{
	new target, amount, price;
	if(playerVariables[playerid][pJob] != 5) return SS(playerid, COOL_GREEN, "You're not a drugs dealer.", "Nu esti un dealer de droguri.");
	if(sscanf(params, "rii", target, amount, price)) return SCM(playerid, COLOR_GREY, "Usage: {FFFFFF}/selldrugs [playerid] [amount] [price]");
	if(HasSentDrugsTo(playerid, target)) return SS(playerid, COOL_GREEN, "You already have sent an offer to that user.", "Ai trimis deja o oferta jucatorului respectiv.");
	new sd = FreeDrugsSlot();
	SellDrugs[sd][drugSender] = playerid;
	SellDrugs[sd][drugReceiver] = target;
	SellDrugs[sd][drugPrice] = price;
	SellDrugs[sd][drugAmount] = amount;
	format(szMessage, sizeof(szMessage), "You have offered %s %dg drugs for $%s.", GetName(target), amount, NumberFormat(price));
	SCM(playerid, COOL_GREEN, szMessage);
	format(szMessage, sizeof(szMessage), "* %s has offered you %dg drugs for $%s.", GetName(playerid), amount, NumberFormat(price));
	SCM(target, COOL_GREEN, szMessage); 
	return 1;
}
Now, I have looped the enum to set some variables to INVALID_PLAYER_ID using y_hooks.
Yes, I have defined y_hooks on top of the script.
Now whenever I try to sell the drugs, I always get the "You already have sent an offer to that user." message and I don't know why. I'm checking if the user sent an offer using the stock HasSentDrugsTo.


Re: I can't get this to work.. - Konstantinos - 09.12.2016

HasSentDrugsTo function has issues. One of them is that it always return 0 as a value (if index 0 is equal to targetid or is not (break; is used) and the last return 0 line is executed.

Use an alternate default value such as -1 if no results found and remove break; completely from the loop. It will stop the loop, you don't want that because unless the loop is finished you cannot know if any player has sent drugs.


Re: I can't get this to work.. - Vince - 09.12.2016

FreeDrugsSlot suffers from the same problem as well. That loop will only run once because of the break.


Re: I can't get this to work.. - danielpalade - 09.12.2016

Thanks! Fixed it!