I am kinda new to lua and I been using it to code some random scripts for a game. On my adventure, I am facing a strange situation with lua variable ambients.
I have declared the following variable:
local spawnWaterZone = {
vector3(3529.44, 3697.76, 20.99),
vector3(3520.59, 3674.07, 20.99),
}
and I have the following piece of code:
<!-- language:lua-->
RegisterNetEvent('humaneHeist:guardsN', function(spawnzoneData)
spawnzone = spawnzoneData.arg1
guardsN = spawnzoneData.arg2
for i=guardsN, 1, -1 do
guardID = CreatePed(30, -1275859404, spawnzone[i], 0, true, false)
print(spawnzone[i] .. ' is the coords')
allGuards[#allGuards+1] = guardID
end
TriggerClientEvent('humaneHeist:guards', -1, allGuards)
end)
I am invoking the function above from another file. In this file, I have defined the Data structure I am sending to the function and the arg1 variable aswell
read it:
arg1 = 'spawnWaterZone',
the thing is, when I execute the code calling the argument passed in the function call, it doesn't loop through the vectors in my local variable 'spawnWaterZone' named the same as the arg1. In fact, the spawnzone[i] is nil.
but if i simply define
RegisterNetEvent('humaneHeist:guardsN', function(spawnzoneData)
spawnzone = spawnWaterZone
the loop works normally and go through the vectors.
Any ideas on why it happens?
For anybody who might stumble in the same question as I:
So basically
arg1 = 'spawnWaterZone',
is not the same asspawnzone = spawnWaterZone
and that is a little bit obvious, but if you tryarg1 = spawnWaterZone
that will not work too.That is because you are calling up a variable that is not defined in the file. You need to:
or