Corona SDK 'nil value' issue - lua

I am making a shooter game using the corona sdk...
My problem is that i am trying to delete an object(that is part of an array)when it leaves the screen.. when i do, i get an error that says 'trying to compare nil value to variable' which refers to a simple move function for every object in the array. Here is relevant parts of code:
function addAlien()
listeners('add')
end
function listeners(action)
if(action == 'add') then
Runtime:addEventListener('enterFrame',update)
enemyTimer = timer.performWithDelay(800,addEnemy,0)
else
Runtime:removeEventListener('enterFrame',update)
timer.cancel(enemyTimer)
end
end
function addEnemy(e)
enemy = display.newImage('drone.png')
enemy.x = 500
enemy.y = math.floor(math.random()*300)
enemy:scale(-0.1,0.1)
enemy.speed = math.random(2,6)
enemies.insert(enemies,enemy)
enemy.enterFrame = moveEnemy
Runtime:addEventListener('enterFrame',enemy)
end
function moveEnemy(self,event)
--if self.x < 100 then
---self:removeSelf()
--self = nil
--removeSelf()
--else
self.x = self.x-self.speed
--end
end
function update(e)
if(enemies.numChildren ~= 0)then
for i = 1,enemies.numChildren do
if(enemies[i] ~= nil)then
--enemies[i].x = enemies[i].x-3
if(enemies[i].x<100)then
--enemies:remove(enemies[i])
--display.remove(enemies[i])
--enemies[i] = nil
end
end
end
end
end
I have commented out the parts that give me errors.
Any help would be appreciated,
thanks

You might want to try and put the delete code in it's own function and then use a timer to remove it so that the function where you are currently deleting the object can return and you're not deleting yourself.
Another option would be to make it temporarily invisible and then loop through the table periodically and remove anything outside of the move handler.

You have a problem with the following code:
function listeners(action)
if(action == 'add') then
Runtime:addEventListener('enterFrame',update)
enemyTimer = timer.performWithDelay(800,addEnemy,0)
else
Runtime:removeEventListener('enterFrame',update)
timer.cancel(enemyTimer)
end
end
You only want to Runtime:addEventListener('enterFrame', update) once. If you do it everytime you add an enemy things will go wrong. The problem is your code runs again and again after the objects have been removed already.

hmmm. did you use the scene template? if so, you should only put the :removeself () and = nil values in the scene destroy section at the bottom. you also dont need the second removeself that is not attached to an object as that is probably the nil issue.
to summarize.
1-put all remove self () and = nil at the destroy section of scene template and use object.isVisible = false instead.
2-the nil error is most likely coming from the removeself() statement with no object. so change it to moveenemy:removeself ()

if my understanding is right this is how i would do it without using runtime
and using timer instead. ask away if you have question
local scrWidth = display.actualContentWidth
local scrHeight = display.actualContentHeight
local enemy = {} --this will hold your aliens
function addEnemy()
enemy[#enemy + 1] = display.newImage("drone.png")
enemy[#enemy].x = 500
enemy[#enemy].y = math.floor(math.random()*300)
enemy[#enemy]:scale(-0.1,0.1)
enemy[#enemy].speed = math.random(2,6)
end
local function update()
addEnemy()
--this will move the enemy to the left from right
for i=1,#enemy,1 do
enemy[i].x = enemy[i].x - enemy[i].speed
end
--the below codes will destroy each enemy that is out side the screen on the left
local function destroyWhenOutside()
for i=1,#enemy,1 do
if enemy[i].x < 0 - enemy[i].width then
enemy[i]:removeSelf()
enemy[i] = nil
elseif enemy[i].y < 0 - enemy[i].height then
enemy[i]:removeSelf()
enemy[i] = nil
end
end
end
destroyWhenOutside()
end
--this will loop the update every 1/1000 seconds
local timerUpdate = timer.performWithDelay(1,update,-1)
.

Related

How can I send arguments through to a client from server using RemoteEvent? ROBLOX

My goal here is to send the player object and the object name through to the client script to display the object name on a text label.
When I run this, it prints nil and nil. I want player.Name and name as you will see in the second code sample. Another error I get is "attempt to concatenate nil with string" from the client script.
Here is my server-side code:
script.onTouch.OnInvoke = function(button, sellObj, sellObjValue, buyObj, buyObjValue, afterWave, isLoadedPart)
if isLoadedPart == true then
local info = script.Parent.Parent.info
local player = info.player.Value
local owner = info.owner.Value
local savedItems = info.savedItems.Value
local builds = script.Parent.Parent.activeBuilds
if afterWave > info.activeWave.Value then
info.activeWave.Value = afterWave
end
button.Parent = savedItems.buttons
button.jobDone.Value = true
if sellObj ~= nil then
sellObj.Parent = savedItems.builds
end
if buyObj ~= nil then
buyObj.Parent = builds
end
local td = require(script.Parent.tycoonDictionary)
if not table.find(td.boughtButtons, button.objectId.Value) then
table.insert(td.boughtButtons, button.objectId.Value)
end
local ui = game.ReplicatedStorage:FindFirstChild('onPartLoad')
if ui then
ui:FireClient(player, 'buyObj.Name')
print('yes')
else
print('no')
end
else
local info = script.Parent.Parent.info
local player = info.player.Value
local owner = info.owner.Value
local money = info.player.Value.leaderstats.Money
local savedItems = info.savedItems.Value
local builds = script.Parent.Parent.activeBuilds
if money.Value >= buyObjValue or money.Value == buyObjValue then
if afterWave > info.activeWave.Value then
info.activeWave.Value = afterWave
end
button.Parent = savedItems.buttons
button.jobDone.Value = true
if sellObj ~= nil then
sellObj.Parent = savedItems.builds
money.Value += sellObjValue
end
if buyObj ~= nil then
buyObj.Parent = builds
money.Value -= buyObjValue
end
local td = require(script.Parent.tycoonDictionary)
if not table.find(td.boughtButtons, button.objectId.Value) then
table.insert(td.boughtButtons, button.objectId.Value)
warn(td.boughtButtons)
end
else
player.PlayerGui.inGame.error.label.invokeScript.errorInvoke:Invoke("Insufficient Funds")
end
end
script.Parent.waveChecker.afterRun:Invoke()
end
And here is my client-side code:
game.ReplicatedStorage.onPartLoad.OnClientEvent:Connect(function(player, name)
print(player.Name, name)
print(script.Parent.Text)
script.Parent.Text = name .. 'is loaded.'
print(script.Parent.Text)
end)
Here I will tell you a little about this game. It is a tycoon that saves data using a table with all button Ids in it. When it loads, it gets the button associated with the id and fires the server code for every button. If the button is a load button, it fires the client with the player and the buyObj.Name.
Is there just a little mistake or can I not send arguments to the client at all? Any help will be appreciated!
The OnInvoke callback's first argument is always the player that fired it, so you should change line 1 of the first script to:
script.onTouch.OnInvoke = function(playerFired, button, sellObj, sellObjValue, buyObj, buyObjValue, afterWave, isLoadedPart)
And :FireClient() requires a player argument as its first argument, so you should change
ui:FireClient(player, 'buyObj.Name')
to
ui:FireClient(playerFired, player, 'buyObj.Name')
Here is the solution I came up with;
First, I read through some Roblox documentation to find that the first argument I had to send when using :FireClient was the player, and because I already had the player there, it was just sending the function to that player. Now, I had 2 choices, I could send the player twice, or delete the player variable from the script. I chose the second one.
Here is what the :FireClient line in the server script looks like now:
game.ReplicatedStorage:WaitForChild('onPartLoad'):FireClient(player, buyObj.Name)
And here is what the client function script looks like now:
game.ReplicatedStorage.onPartLoad.OnClientEvent:Connect(function(name)
if name ~= 'starterFoundation' then
script.Parent.Text = name .. ' is loaded.'
end
end)
Thank you #TypeChecked for helping me realize this!

How to make NPC in roblox walk around normally then when it sees a player it runs after them

The titles explains it all, I'm trying to make a roblox npc walk around the maze normally, then when it sees the player, it starts running after the player to kill it, I have the NPC, I have the killing part, I just need the code for the NPC walking around normally and the code for the NPC detecting the player then running after them. Thanks! :D
edited: heres my code
debugMode = false
targetNPCs = false
--
h = script.Parent.Parent:WaitForChild("Humanoid")
pathService = game:GetService("PathfindingService")
targetV = script.Parent:WaitForChild("Target")
function closestTargetAndPath()
local humanoids = {}
if targetNPCs then
local function recurse(o)
for _,obj in pairs(o:GetChildren()) do
if obj:IsA("Model") then
if obj:findFirstChild("Humanoid") and obj:findFirstChild("Torso") and obj.Humanoid ~= h and obj.Humanoid.Health > 0 and not obj:findFirstChild("ForceField") then
table.insert(humanoids,obj.Humanoid)
end
end
recurse(obj)
end
end
recurse(workspace)
else
for _,v in pairs(game.Players:GetPlayers()) do
if v.Character and v.Character:findFirstChild("HumanoidRootPart") and v.Character:findFirstChild("Humanoid") and v.Character.Humanoid.Health > 0 and not v:findFirstChild("ForceField") then
table.insert(humanoids,v.Character.Humanoid)
end
end
end
local closest,path,dist
for _,humanoid in pairs(humanoids) do
local myPath = pathService:ComputeRawPathAsync(h.Torso.Position,humanoid.Torso.Position,500)
if myPath.Status ~= Enum.PathStatus.FailFinishNotEmpty then
-- Now that we have a successful path, we need to figure out how far we need to actually travel to reach this point.
local myDist = 0
local previous = h.Torso.Position
for _,point in pairs(myPath:GetPointCoordinates()) do
myDist = myDist + (point-previous).magnitude
previous = point
end
if not dist or myDist < dist then -- if true, this is the closest path so far.
closest = humanoid
path = myPath
dist = myDist
end
end
end
return closest,path
end
function goToPos(loc)
h:MoveTo(loc)
local distance = (loc-h.Torso.Position).magnitude
local start = tick()
while distance > 4 do
if tick()-start > distance/h.WalkSpeed then -- Something may have gone wrong. Just break.
break
end
distance = (loc-h.Torso.Position).magnitude
wait()
end
end
while wait() do
local target,path = closestTargetAndPath()
local didBreak = false
local targetStart
if target and h.Torso then
targetV.Value = target
targetStart = target.Torso.Position
roaming = false
local previous = h.Torso.Position
local points = path:GetPointCoordinates()
local s = #points > 1 and 2 or 1
for i = s,#points do
local point = points[i]
if didBreak then
break
end
if target and target.Torso and target.Health > 0 then
if (target.Torso.Position-targetStart).magnitude < 1.5 then
local pos = previous:lerp(point,.5)
local moveDir = ((pos - h.Torso.Position).unit * 2)
goToPos(previous:lerp(point,.5))
previous = point
end
else
didBreak = true
break
end
end
else
targetV.Value = nil
end
if not didBreak and targetStart then
goToPos(targetStart)
end
end
I won't provide any code as you have not shown any efforts to solve that problem.
Enter "roblox npc" into www.google.com. Click the second hit:
https://developer.roblox.com/en-us/articles/Moving-NPCs-Between-Points
This covers the walking around part and also links this
https://developer.roblox.com/en-us/articles/Pathfinding
which covers more complex movements.
The seeing part is done with ray casting
https://developer.roblox.com/en-us/articles/Raycasting
If you can cast a ray from the NPC to your player calculate a path to that player and attack.

How can I dynamically retrieve the variable used to initialize an instance?

I'm trying to implement a speed-based turn system for a roguelike. I've set up a Mob class using metamethods, so that assigning the following to a variable will spawn a mob into the map at certain grid coordinates:
function Mob:spawn(x,y,m)
local mob = {}
setmetatable(mob, Mob)
mob.x = x
mob.y = y
mob.is_monster = m
return mob
end
Once that's done, I call the following:
function Mob:roll_call()
who_is_here[self.y][self.x] = self.is_monster
self.turn_counter = self.turn_counter * math.random(0.9, 1.1)
table.insert(allTurnCounters, self.turn_counter)
end
This puts the mob's self.turn_counter into a table. Meanwhile, in another module, I've defined these two functions, the heart of the problem:
function turn.decrement_counters(dt) -- runs in Dungeon.update(dt) and subtracts from allTurnCounters
for i = 1,#allMobsSpawned do
if allTurnCounters[i] <= 0 then
allTurnCounters[i] = 0
turn_active = true
whose_turn = i
return
elseif allTurnCounters[i] > 0 then
allTurnCounters[i] = allTurnCounters[i] - (10 * dt)
end
end
end
function turn.whose_is_it() -- called when an entry in allTurnCounters goes zero
if whose_turn == 1 then -- spots 1 and 2 in the spawn list are only ever for players
player1.my_turn = true -- turns on player 1's keys
elseif whose_turn == 2 then
player2.my_turn = true -- turns on player 2's keys
elseif whose_turn >= 3 then -- above 3 we're in monster territory
end
end
I've decided that the first two instances of Mob to be initialized will always be players 1 and 2, assigned to the variables player1 and player2, respectively. And, as it is, it works fine for passing control back and forth between players! But obviously, that's not enough for a fully-featured game. I need monsters, too.
The allTurnCounters table gets new entries, in order, from every mob that spawns (a class which includes both the players and the monsters, so they can share stats). Here's my question: How can I get Lua to dynamically retrieve the name of the table associated with a given turn_counter/value within that table, and use it to take turn priority, even in the event that I don't know what's been procedurally spawned ahead of time or what place it will occupy in the spawn order?
I have 3 ideas, none of which I'm solid on how to implement. One method would be something like sending the entire instance table to another table, rather than just their turn_counters, and then somehow grabbing a pair of values (the table itself and my_turn within the table), updating my_turn's value directly.
Another method might be to use the environment _G... somehow. I'm still poring over Chapter 14 of PiL trying to adapt it to my purposes, but value = _G[varname] seems to be a powerful bit of code I might be able to use for this. Not sure how, just yet.
My last idea was to maybe write some kind of string-sensing find-replace that can grab some other value in each mob's table and then pop it on the front of my_turn. Like, assigning some value with a known pattern for each mob type, that I can use in a string.find and then a string.gsub, to like... manually make the code line read as intended. Seems inelegant, though.
I had good luck with asking my previous Lua/Love2D question here, so I figured let's toss it out there while I'm thinking!
Here is my suggestion for how you should implement this:
Instead of allTurnCounters[i], give mobs a turn_counter property and use allMobsSpawned[i].turn_counter. Then, delete allTurnCounters.
Instead of storing the mob number in whose_turn, store the mob itself. (Note: when I say "the mob itself", it's short for "a reference to the mob itself")
So instead of
whose_turn = i
you would have:
whose_turn = allMobsSpawned[i]
Now whose_turn holds the mob whose turn it is. You can easily check whose_turn == player1, whose_turn == player2, etc. As a bonus, it doesn't rely on the players being the first mobs any more.
You can access the mob's properties through whose_turn - if whose_turn == player1 is true for example then whose_turn.x accesses the same field as player1.x
Here's a somewhat janky solution that can be made more elegant by incorporating the method from the other answer. This is what I came up with on my own while waiting for an answer.
-- in "Mob.lua" module
function Mob:roll_call()
who_is_here[self.y][self.x] = self.is_monster
self.turn_counter = self.turn_counter * math.random(0.9, 1.1)
table.insert(allMobs, {self.name, self.turn_counter})
if self.is_monster == true then
table.insert(allMonsters, {self.name, self.turn_counter})
end
end
function Mob:move(dx, dy)
who_is_here[self.y][self.x] = nil
self.x, self.y = self.x + dx, self.y + dy
who_is_here[self.y][self.x] = self.is_monster
self.turn_counter = 1 * self.speed
for k,v in ipairs(allMobs) do
if v[1] == self.name then
v[2] = self.turn_counter
end
end
self.my_turn = false -- flags turn is over
turn_active = false -- flags no active turn
end
-- in "turn.lua" module
function turn.decrement_counters(dt)
if turn_active == false then
for k,v in ipairs(allMobs) do
v[2] = v[2] - (10 * dt)
if v[2] < 0 then
v[2] = 0
turn_active = true
whose_turn = v[1]
return
end
end
else turn.whose_is_it()
end
end
function turn.whose_is_it()
if whose_turn == player1.name then
player1.my_turn = true
elseif whose_turn == player2.name then
player2.my_turn = true
elseif whose_turn == ant1.name then
ant1.my_turn = true
end
end
turn.whose_is_it() is the part that will need refining. If I use immibis' method of assigning allMobs[i].turn_counter, that will simplify things considerably and allow for future expansion. This answer only works for player1, player2, and an ant called ant1 in particular.

Cloning+Dragging model when button is click ROBLOX [LUA]?

So I thought of having this for so long, I just don't know where to start. I am new to this language and I keep learning, but kind of hard for me. But I have built my very own custom character which took 2 weeks for me. Anyway, For my question. An example is if I have a button and I click it, a model will be clone and I can drag that model and put it anywhere nearby. What possible method I can use to achieve this?
First things first, I suggest for any future questions, you head over to https://scriptinghelpers.org/
now, on to your question, for cloning the model, you should use mouse.Target.Parent:Clone() or the GetTopParent(mouse.Target) function in my function library (which you can get here; http://www.roblox.com/item.aspx?id=244244638)
then deposit the model into workspace and MakeJoints()
the next step is to move the model, this can be tricky, but the simplest method is model:MoveTo(mouse.Hit.p) on mouse.Moved (but that's a little buggy)
Another method for movement would be to use the Handles class, but I'm not really familiar with it, so you'd have to figure that one out on your own.
To make the first method less buggy, I'd suggest something along the lines of
model:MoveTo(mouse.Hit.p.X, mouse.Target.Position.Y + (model:GetExtentsSize().Y / 2), mouse.Hit.p.Z)
but you'd have to set up the mouse to ignore the model, which I can't really help with.
A really good place to start is to search the free models in Studio Toolbox for a 'Dragger Tool' or 'Model Dragger Tool' and then use the script inside to get started creating your own. I have learned to create my own custom draggers by doing this and it is way easier than you may think at first. Once you find a good dragger tool to borrow code from, if you need to enhance it, you can find the dragger api in the Roblox Wiki to help you further customize it to your specific needs.
http://wiki.roblox.com/index.php?title=API:Class/Dragger
EDIT: So here's the first dragger script that showed when I searched. It will drag models and parts but you will have to edit it to meet your requirements using the dragger api. Create a Tool in player.BackPack then create a LocalScript inside the Tool then copy and paste the code below into the LocalScript, and that will get you started.
local Tool = script.Parent
enabled = true
local origTexture = Tool.TextureId
game:GetService("ContentProvider"):Preload("rbxasset://icons/freemove_sel.png")
local selectionBox
local currentSelection
local currentSelectionColors = {}
local selectionLasso
local inGui = false
local inPalette = false
local lockTime = 0
function canSelectObject(part)
return part and not (part.Locked) and (part.Position - script.Parent.Parent.Head.Position).Magnitude < 60
end
function findModel(part)
while part ~= nil do
if part.className == "Model" then
return part
end
part = part.Parent
end
return nil
end
function startDrag(mousePart, hitPoint, collection)
dragger = Instance.new("Dragger")
pcall(function() dragger:MouseDown(mousePart, hitPoint, collection) end)
end
function collectBaseParts(object, collection)
if object:IsA("BasePart") then
collection[#collection+1] = object
end
for index,child in pairs(object:GetChildren()) do
collectBaseParts(child, collection)
end
end
function onMouseDown(mouse)
mouse.Icon ="rbxasset://textures\\GrabRotateCursor.png"
local part = mouse.Target
if canSelectObject(part) then
local hitPoint = mouse.Hit:toObjectSpace(part.CFrame).p
if trySelection(part) then
local instances = {}
collectBaseParts(currentSelection, instances)
startDrag(part, hitPoint, instances)
return
end
end
--Clear the selection if we weren't able to lock succesfullu
onMouseUp(mouse)
end
function onMouseUp(mouse)
mouse.Icon ="rbxasset://textures\\GrabCursor.png"
if dragger ~= nil then
pcall(function() dragger:MouseUp() end)
dragger = nil
end
end
function trySelection(part)
if canSelectObject(part) then
selectionLasso.Part = part
local model = findModel(part)
if model then
return setSelection(model)
else
return setSelection(part)
end
else
clearSelection()
return false
end
end
function onKeyDown(key)
if dragger ~= nil then
if key == 'R' or key == 'r' then
dragger:AxisRotate(Enum.Axis.Y)
elseif key == 'T' or key == 't' then
dragger:AxisRotate(Enum.Axis.Z)
end
end
end
local alreadyMoving
function onMouseMove(mouse)
if alreadyMoving then
return
end
alreadyMoving = true
if dragger ~= nil then
--Maintain the lock
if time() - lockTime > 3 then
Instance.Lock(currentSelection)
lockTime = time()
end
--Then drag
pcall(function() dragger:MouseMove(mouse.UnitRay) end)
else
trySelection(mouse.Target)
end
alreadyMoving = false
end
function saveSelectionColor(instance)
if instance:IsA("BasePart") then
currentSelectionColors[instance] = instance.BrickColor
if instance.BrickColor == BrickColor.Blue() then
instance.BrickColor = BrickColor.new("Deep blue")
else
instance.BrickColor = BrickColor.Blue()
end
end
local children = instance:GetChildren()
if children then
for pos, child in pairs(children) do
saveSelectionColor(child)
end
end
end
function setSelection(partOrModel)
if partOrModel ~= currentSelection then
clearSelection()
if Instance.Lock(partOrModel) then
lockTime = time()
currentSelection = partOrModel
saveSelectionColor(currentSelection)
selectionBox.Adornee = currentSelection
return true
end
else
if currentSelection ~= nil then
if time() - lockTime > 2 then
--Maintain the lock
if not(Instance.Lock(currentSelection)) then
--we lost the lock
clearSelection()
return false
else
lockTime = time()
return true
end
else
return true
end
end
end
return false
end
function clearSelection()
if currentSelection ~= nil then
for part, color in pairs(currentSelectionColors) do
part.BrickColor = color
end
selectionBox.Adornee = nil
Instance.Unlock(currentSelection)
end
currentSelectionColors = {}
currentSelection = nil
selectionLasso.Part = nil
selectionBox.Adornee = nil
end
function onEquippedLocal(mouse)
Tool.TextureId = "rbxasset://icons/freemove_sel.png"
local character = script.Parent.Parent
local player = game.Players:GetPlayerFromCharacter(character)
inGui = false
inPalette = false
mouse.Icon ="rbxasset://textures\\GrabCursor.png"
mouse.Button1Down:connect(function() onMouseDown(mouse) end)
mouse.Button1Up:connect(function() onMouseUp(mouse) end)
mouse.Move:connect(function() onMouseMove(mouse) end)
mouse.KeyDown:connect(function(string) onKeyDown(string) end)
selectionBox = Instance.new("SelectionBox")
selectionBox.Name = "Model Delete Selection"
selectionBox.Color = BrickColor.Blue()
selectionBox.Adornee = nil
selectionBox.Parent = player.PlayerGui
selectionLasso = Instance.new("SelectionPartLasso")
selectionLasso.Name = "Model Drag Lasso"
selectionLasso.Humanoid = character.Humanoid
selectionLasso.archivable = false
selectionLasso.Visible = true
selectionLasso.Parent = game.workspace
selectionLasso.Color = BrickColor.Blue()
alreadyMoving = false
end
function onUnequippedLocal()
Tool.TextureId = origTexture
clearSelection()
selectionBox:Remove()
selectionLasso:Remove()
end
Tool.Equipped:connect(onEquippedLocal)
Tool.Unequipped:connect(onUnequippedLocal)

Delete object[i] from table or group in corona sdk

i have a problem (obviusly :P)
i'm create a mini game, and when i touch a Object-A , creates an Object-B.
If i touch N times, this create N Object-B.
(Object-B are Bubbles in my game)
so, i try when I touch the bubble (object-B), that disappears or perform any actions.
I try adding Object-B to Array
local t = {}
.
.
.
bur = display.newImage("burbuja.png")
table.insert(t,bur)
and where i have my eventListeners i wrote:
for i=1, #t do
bur[i]:addEventListener("tap",reventar(i))
end
and my function 'reventar'
local function reventar (event,id)
table.remove(t,id)
end
i'm lost, and only i want disappears the bubbles.
you're probably gonna want to do something like this:
local t = {}
bur = display.newImage("burbuja.png")
table.insert(t,bur)
-- declaring the function first so it can be used later in the for loop
local function reventar(event)
t[event.target.id] = nil -- We remove object from table
event.target:removeSelf() -- Also remember to remove from display
end
for i=1,#t do
t[i].id = i
t[i]:addEventListener("tap", reventar)
end
Hope this helps.
EDIT
I would do it this way, because it's better when you want to loop through the objects:
local t = {}
-- declaring the function first so it can be used later
local function reventar(event)
event.target.kill = true -- Mark the clicked object for later destruction
end
bur = display.newImage("burbuja.png")
bur:addEventListener("tap", reventar)
table.insert(t,bur)
local function loop(event)
for i = #t, 1, -1 do
local object = t[i]
-- Do stuff to object here, such as object.y = object.y + 1
if object.kill then -- Check if object is marked for destruction
local child = table.remove(t, i) -- Remove from table
if child ~= nil then
-- Remove from display and nil it
child:removeSelf()
child = nil
end
end
end
end
Runtime:addEventListener("enterFrame", loop) -- Remember to remove this when no longer needed

Resources