View Full Version : attempt to perform arithmetic (userdata)

10-12-2014, 01:25 PM
Hello got a little problem on a script, the script readsout 3 Coordinates from an SQL DB and use a function to get if the player is in certain area defined by this 3 Coordinates.

so this is the code to readout the first X coordinate:
p0_x = WorldDBQuery("SELECT X1 FROM EventArea WHERE Map = " ..plr_map.. " AND AreaID= " ..area.. " ");

and it sends it to this:
local function ptInTriangle(p_x, p_y, p0_x, p0_y, p1_x, p1_y, p2_x, p2_y)
local A = 1/2 * (-p1_y * p2_x + p0_y * (-p1_x + p2_x) + p0_x * (p1_y - p2_y) + p1_x * p2_y);

but i just got an error that says: attempt to perform arithmetic on local p1_y (a userdata value)

so whats wrong? Do I need to convert the float of the SQL db to any other form of data to work within lua?

10-12-2014, 02:28 PM
The way SQL queries work is that
you run the SQL, get the result object or nil.
Then you can get info from the object.
The object contains all the rows and columns from the SQL query.


local entry = 123123
local Q = WorldDBQuery("SELECT npcflag, name FROM creature_template WHERE entry = "..entry)
if(Q) then
local npcflag = Q:GetUInt32(0)
local name = Q:GetString(1)
print(name, npcflag)
print("No rows found or there was some error")

The indexes start from 0, so GetUInt32(0) gets the NPCflag
You should usually always check if the result, Q in this case, is nil and handle that case or you may get nil errors that can cripple the script.

The error in your script is that you are using Q directly in the math.
This means that you are trying to for example do 100 + player, which doesnt work obviously as player is not a number.
So what you need to do is to get the value with a function from the query result and use that in the math.

See this for more:

10-12-2014, 05:36 PM
ok thanks got this it works now.

a little question, how do i use the Method WorldObject:GetNearObject correct to get an Guid from an GameObject?

obj = player:GetNearObject(533, 0, 0, 0); (Whats the Configuration for Range and Type to get an GameObject)
objguid = obj:GetGuid(); (Error Message get a nil Value)
objid = obj:GetEntry();

Edit: oh also tried it with:
obj = player:GetNearestGameObject(533, 0);
got only nil values for Guid...

10-12-2014, 05:59 PM
You should use obj:GetGUID()
GUID is all capital.