# Community Forum

Forum Overview >> Scripting

 Category Scripting Created 07.05.2021 14:53

Tomas Roy (TomRoyo) 07.05.2021 14:53
Hi,

How can I check if a player is in an unload trigger, for example in a silo or cowshed unload trigger?

Bilbo Beutlin (BBeutlin) 08.05.2021 01:38
Since there's no player collision mask detection by these triggers, you'll need to scan the station/trigger tables whether the player is within a certain range.

Tomas Roy (TomRoyo) 08.05.2021 16:42
Thank you for your response Bilbo. Am I going in the right direction?

local ax, ay, az= getWorldTranslation(g_currentMission.player)
local bx, by, bz= getWorldTranslation(trigger.triggerId)
local distance = MathUtil.vector3Length(ax-bx, ay-by, az-bz)

if distance < 1 then
-- do something
end
end

Bilbo Beutlin (BBeutlin) 08.05.2021 22:22
no no no .. 'g_currentMission.player' is a table, 'trigger.triggerId' is the user-defined location of the map icon.
Simpliest way is
local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode);

Tomas Roy (TomRoyo) 09.05.2021 22:41
Thank you, Bilbo, for the correction. I have one more question, because if I use :

local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode)
print(distance)
end

the function calculates the player's distance from the building (silo, point of sale) and not from the unload trigger. On the other hand, when I use the function:

for _, trigger in pairs(g_currentMission.unloadTriggers) do
local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode)
print(distance)
end

I get an error: bad argument #1 to 'pairs' (table expected, got nil)

Bilbo Beutlin (BBeutlin) 10.05.2021 00:00

However there's a table 'g_currentMission.tipTriggers'. This should be suitable (no warranty).

Tomas Roy (TomRoyo) 10.05.2021 11:41
Unfortunately it seems not work, but I guess I made something wrong:

local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode)
print(distance)
end

with this I get error -> attempt to index field 'unloadingStations' (a nil value)

and with this:

local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode)
print(distance)
end

I get -> bad argument #1 to 'pairs' (table expected, got nil)

but for:

for _, trigger in pairs(g_currentMission.tipTriggers) do
local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode)
print(distance)
end

it do nothing

Bilbo Beutlin (BBeutlin) 10.05.2021 12:18
I'd suggest you print() out some tables to understand the structures, eg. with the function DebugUtil.printTableRecursively()
See https://gdn.giants-software.com/documentation_scripting_fs19.php?version=script&category=6&class=46#printTableRecursively411