Trouble cloning monster - lua

I have been working on this for a while, but I cant seem to get it to work. The point of this code is to spawn a monster after the intermission. The player then gets a certain amount of time to survive before intermission starts again. This should happen over and over again. Strangely, it immediately skips to spawning him and the timer doesnt run. Any ideas?
while true do
game.Workspace.Monster:Destroy()
for i = 20, 0, 1 do
game.StarterGui.ScreenGui2.Frame.TextLabel.Text = "That wasn't strange at all... Oh well back to our tea... ".. i
wait(1)
end
local monster = game.ServerStorage.Enemy:Clone()
monster.Parent = game.Workspace
for i = 20, 0, 1 do
game.StarterGui.ScreenGui2.Frame.TextLabel.Text = "SURVIVE.. ".. i
wait(1)
end
game.Workspace.Monster:Destroy()
end

You've got a simple mistake : your for-loop never decrements.
You have
for i = 20, 0, 1 do
And it should be
for i = 20, 0, -1 do

Related

Roblox: Parts not being cloned

I'm trying to make a script that summons cars randomly. I haven't seen a car being summoned or any errors in the console.
I tried this code which SHOULD choose one of the random cars and clone them:
function MakeCar(towardPlayers: boolean)
local Car:MeshPart = script[tostring(math.random(1,45))]:Clone()
Car.Anchored = false
if towardPlayers then
Car.Position = Vector3.new(-119.75, 5, -32)
Car.Orientation = Vector3.new(0, -90, 0)
while Car do
Car.Position += Vector3.new(2, 0, 0)
wait(0.01)
end
else
Car.Position = Vector3.new(119.75, 5, -48.25 / 100))
Car.Orientation = Vector3.new(0, 90, 0)
while Car do
Car.Position -= Vector3.new(2, 0, 0)
wait(0.01)
end
end
end
while true do
MakeCar(true)
wait(math.random(100, 10000) / 100)
end
while true do
MakeCar(false)
wait(math.random(100, 10000) / 100)
end
I was expecting a random car to show up and drive on the road, but I get nothing. Not even an output in the console. So I have no idea what's going on in the script.
What might be important:
I double checked the Position and made sure it would land on the road
It is a Script, not a LocalScript
You need to set the car's Parent to the workspace so that the car exists in the game.
Why? Only Parts, Models, ... exist physically in the game when it has it's parent in the workspace.
Car.Parent = workspace

Coroutine problems

so I'm trying to make a basic GUI animation system in ROBLOX, using individual frames and a loop to put them into an imagelabel.
This is the function:
local playAnimation = coroutine.create(function(anim,pos,tank)
while true do
local animBase = sp.AnimBase:Clone()
animBase.Parent = tank
animBase.Visible = true
animBase.Position = pos -- line that causes the error mentioned below.
local frame = 1
for i = 0, animations[anim]["FrameNum"] do
frame = frame + 1
animBase.Image = animations[anim]["Frames"][frame]
NewWait(.1) --this right here, the wait, interfears with the yield.
if frame >= animations[anim]["FrameNum"] then
pos,anim,tank = coroutine.yield()
break
end
end
animBase:Destroy()
end
end)
There are two main problems with this:
every time it runs, I get this error:
20:41:01.934 - Players.Player1.PlayerGui.ScreenGui.Gui-MAIN:65: bad argument #3 to 'Position' (UDim2 expected, got number)
Although this error doesn't seem to do anything. (eg. stop the script completely)
The line causing the error is marked with a comment.
I've made sure that pos is correct. I even tried printing it before setting it, it prints the correct thing which is:
{0,120},{0,65}
The other main problem is that I can't resume it after using it once. It can run this line multiple times fine:
coroutine.resume(playAnimation,"Cannon Fire",UDim2.new(0,120,0,68-25),tank.Frame)
but it won't run:
if tank2:FindFirstChild("Ammo") and isTouching(ammoFrame,tank2:GetChildren()[3]) then
local lastAmmoPos = ammoFrame.Position
ammoFrame:Destroy()
coroutine.resume(playAnimation,"Explosion",lastAmmoPos-UDim2.new(0,25-(ammoTypes[type]["Size"].X.Offset)/2,0,25),tank.Frame)
tank2:GetChildren()[3]:Destroy()
end
Yes, the if statement is working fine. ammoFrame is destroyed and so is the third child of tank2. The coroutine just won't resume.
Fixed by removing the coroutine completely and wrapping the for loop inside a spawn function.
local playAnimation = function(anim,pos,tank)
local animBase = sp.AnimBase:Clone()
animBase.Parent = tank
animBase.Visible = true
animBase.Position = pos
local frame = 1
spawn(function()
for i = 0, animations[anim]["FrameNum"] do
frame = frame + 1
animBase.Image = animations[anim]["Frames"][frame]
wait(.1) --this right here, the wait, interfears with the yield.
if frame >= animations[anim]["FrameNum"] then
break
end
end
animBase:Destroy()
end)
end

Corona transition.moveTo() seems to stop other transitions on the same object?

local rect = display.newRoundedRect(200, 200, 150, 150, 2)
rect.fill = {0,0,0}
rect.alpha = 0.1
rect.xScale = 0.1
rect.yScale = 0.1
transition.to(rect, {time=1000, xScale=1, yScale=1, alpha=1}) -- transA
local function moveListener()
transition.moveTo(rect, {time=800,x=300, y=300}) -- this will puase transA!!
end
timer.performWithDelay(400,moveListener, 1)
Any idea how to perform moveTo() without stopping other transitions?
Cheers!
I just tried this on my computer, and it seems to be an issue with transition.moveTo().
It works using transition.to().
I just tried the following:
local rect1 = display.newRoundedRect(100, 100, 150, 150, 2)
rect1.fill = {0,0,0}
local rect2 = display.newRoundedRect(300, 100, 150, 150, 2)
rect2.fill = {1,0,1}
transition.moveTo(rect1, {time=1000, y=500})
transition.to(rect2, {time=1000, y=500})
And noticed that rect1 moved after rect2 with some delay!. However, adding small delay to transition.to() will make the movement exactly identical as follows:
transition.to(rect2, {delay=1, time=1000, y=500})
To summerize, it is still an issue with moveTo() to stop other transitions. As work around, in order to get exact behavior just use transition.to() with micro delay as previous line of code!
Thanks.....

Corona: go to scene when event loop if else statement variable matches

ok so I have this.
local quotetap = 30
function changet()
if tImage then tImage:removeSelf() end
if counterBlock then counterBlock:removeSelf() end
tImage = display.newImage( "images/tFaces_"..math.random(6)..".jpg", 264, 280 )
tImage.x = display.contentWidth * 0.5
tImage.y = display.contentHeight * 0.5
counter = counter + 1
counterBlock = display.newText(counter, 30, 30, native.systemFont, 25)
counterBlock.x = display.contentWidth /7 *6
counterBlock.y = display.contentHeight /10
tImage:addEventListener("tap", changet)
end
I want to add.
if (counter == quotetap) then
composer.gotoScene( "scenes.nextlevel", "fade", 500 )
end
mind you, the counter works perfectly. When add this however, i notice the friction when the counter hits 30. But it doesn't change scene like it should. After a slight pause, it just continues counting.
ah I've figured out what's happening. Sorry for the meaningless question. Seems it actually is changing scenes. The scene i switch to is currently blank. So it actually is switching, and not clearing the old objects. So it appears that it is staying on the same scene, when it actually is not.
You need to include the parameters (time, effect) in a params table. Look here https://docs.coronalabs.com/api/library/composer/gotoScene.html
Change
composer.gotoScene( "scenes.nextlevel", "fade", 500 )
To
local options = {
effect = "fade",
time = 500,
}
composer.gotoScene( "scenes.nextlevel", options )
Alternatively:
composer.gotoScene("scenes.nextlevel", { effect = "fade", time = 500, })

In corona sdk how can I reference / specify all indexes of a spawned object on the screen?

Hi I've been wondering this for a while and it's causing me lots of problems not knowing more about how to reference / specify all indexes of a spawned enemy on the screen at one time.
Say when my character dies I want all the enemies on the screen at that time to move away from my dead character as the screen fades out. Simply calling 'enemy1' only makes one (the last spawned I think) do as it's told.
Here is my enemy spawn script:
local spawnTable2 = {}
local function spawnEnemy()
enemy1 = display.newSprite( group, sheetE, sequenceData2 )
enemy1.x=math.random(100,1300)
enemy1.y=math.random(360,760)
enemy1.gravityScale = 0
enemy1:play()
enemy1.type="coin"
enemy1.objTable = spawnTable2
enemy1.index = #enemy1.objTable + 1
enemy1.myName = "enemy" .. enemy1.index
physics.addBody( enemy1, "kinematic",{ density = 0, friction = 0, bounce = 1 })
enemy1.isFixedRotation = true
enemy1.type = "enemy1"
enemy1.timer = nil
enemy1.enterFrame = moveEnemy
Runtime:addEventListener("enterFrame",enemy1)
enemy1.objTable[enemy1.index] = enemy1
hudGroup:toFront()
return enemy1
end
To reference all objects, you would need to have two things.
A Counter
Loop
First make a counter:
counter = 0
Every time the funciton is called have a counter:
counter = counter + 1
For tables, you would do something like this:
spawnTable2[counter].x=math.random(100,1300)
Then when you want to remove the object, you would just do this:
display.remove(spawnTable2[counter])
Just keep in mind, everything you do to manipulate that object will have to be inside that function. Good luck and hope this helps.

Resources