SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 24/04/2015, 07:16 PM   #1
rOps
Big Clucker
 
Join Date: Apr 2014
Posts: 71
Reputation: 0
Default Accessing element at negative index -400

HTML Code:
[22:07:56] [debug] Run time error 4: "Array index out of bounds"
[22:07:56] [debug]  Accessing element at negative index -400
[22:07:56] [debug] AMX backtrace:
[22:07:56] [debug] #0 000223ec in public OnDialogResponse () from LTGRP.amx
Code:
new String[128];

format(String, sizeof(String), "{94CB80} Pardavėte transportą {ff0000}%s {94CB80}u {ff0000}%s{94CB80}.", VehicleNames[GetVehicleModel(vehicleid) - 400], FormatuotiSkaicius(MoneyCount));
SendClientMessage(playerid, -1, String);
HTML Code:
stock FormatuotiSkaicius(Number)
{
    new String[128];

    format(String, sizeof(String), "%d", Number);

    if(-1000 < Number < 1000) return String;

    new Minus = 0;

    if(Number < 0) Minus = 1;

    new Length = strlen(String);

    while((Length -= 3) > Minus) strins(String, ",", Length);

    return String;
}
rOps is offline   Reply With Quote
Old 24/04/2015, 07:19 PM   #2
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,843
Reputation: 1399
Default Re: Accessing element at negative index -400

The vehicle does not exist (invalid) so GetVehicleModel returns 0. You can prevent it and if the vehicle does not exist, it'll print "N/A" instead:
pawn Code:
new String[128], modelid = GetVehicleModel(vehicleid);

format(String, sizeof(String), "{94CB80} Pardavėte transportą {ff0000}%s {94CB80}u {ff0000}%s{94CB80}.", (modelid) ? (VehicleNames[modelid - 400]) : ("N/A"), FormatuotiSkaicius(MoneyCount));
SendClientMessage(playerid, -1, String);

However, I'd suggest to check if the modelid is not valid and don't send the message at all.
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 24/04/2015, 07:24 PM   #3
rOps
Big Clucker
 
Join Date: Apr 2014
Posts: 71
Reputation: 0
Default Re: Accessing element at negative index -400

Quote:
Originally Posted by Konstantinos View Post
The vehicle does not exist (invalid) so GetVehicleModel returns 0. You can prevent it and if the vehicle does not exist, it'll print "N/A" instead:
pawn Code:
new String[128], modelid = GetVehicleModel(vehicleid);

format(String, sizeof(String), "{94CB80} Pardavėte transportą {ff0000}%s {94CB80}u {ff0000}%s{94CB80}.", (modelid) ? (VehicleNames[modelid - 400]) : ("N/A"), FormatuotiSkaicius(MoneyCount));
SendClientMessage(playerid, -1, String);

However, I'd suggest to check if the modelid is not valid and don't send the message at all.
Thanks, it works. But why in another code it works too (as on the my example)?
rOps is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing element at index 65535 zaibaslr2 Scripting Help 5 03/04/2015 10:28 PM
Accessing element at negative index -1 dan40o Scripting Help 9 17/03/2015 05:21 PM
Accessing element at negative index -400 nGen.SoNNy Scripting Help 5 18/11/2014 01:26 PM
Accessing element at negative index -400 liinor Server Support 6 10/10/2014 01:54 PM
Accessing element at negative index -400 Vvolk Scripting Help 2 29/10/2012 08:25 PM


All times are GMT. The time now is 09:40 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.