Powerup - How To Go Back To Normal Condition After X Seconds?

hi guys, so i have created powerup in my game. Basically when the player hits the powerup, his speed will increase. Question is, how do I set up this “temporary” increase to be only for x seconds?

What i have for now is
local speed = 8

and when the player hits the powerup:
local speed = 40

what do i need to add in the powerup function so this “40” speed would stay only for X seconds and the speed will go back to “8” later?

*I also would like to add image like shield animation around the player character when he gets the powerup and this shield animation gets deleted when the powerup effect has worn off, how do I do that? [import]uid: 114765 topic_id: 20700 reply_id: 320700[/import]

Something like this?

[lua]local shield
local powerUpOn = false
local speed = 8
local powerUpLength = 5
local collectedPowerUp = false
local powerUp = function ()

speed = 40
shield = display.newImageRect(“shield.png”,50,50) – replace with anim
shield.alpha = 0.5
shield.x = player.x; shield.y = player.y
powerUpOn = true

local endPowerUp = function ()

speed = 8
shield:removeSelf()
shield = nil
powerUpOn = false

end

timer.performWithDelay(powerUpLength*1000,endPowerUp)

end
– put this within your gameloop

local gameLoop = function ()

if collectedPowerUp then

powerUp()
collectedPowerUp = false
end
if powerUpOn then

shield.x = player.x
shield.y = player.y

end

end

[import]uid: 93133 topic_id: 20700 reply_id: 81278[/import]

--after 1 second  
timer.performWithDelay(1000,function() speed=8 end)  
-- 5000 = 5 seconds.  
--function example  
  
function setSpeed(sp)  
 speed=sp  
end  
  
timer.performWithDelay( 1000,setSpeed(8) )  
  

P.S
using local may return nil.
dont use local if you are using speed variable in other functions etc…
[import]uid: 96683 topic_id: 20700 reply_id: 81281[/import]