Help With Health System in p5.js Game

Alright, so I’ve been slowly making my own little game using p5.js, and I’ve hit a bit of a snag. I have quite a bit set up, including boundaries for the two players, key inputs, and the ability to fire “bullets” at each other and take away the other person’s health by hitting them with said bullet. What I am now trying to implement is a system where when a player’s health is below half, a health item will spawn in which, if touched by the player on that side, will regenerate a certain amount of health. The healing really isn’t the problem because I just used the same logic as taking away health but with the new object giving health. But, I only want the health item to spawn in for about 3 seconds before it disappears. So essentially, there will only be a small window of time to regain health. But when I run the code, every time I collect the item, a new one instantly spawns. I think the problem is in this code:

if(timer.count % 3000 && hw1 < 100 && !healSquares1[0]) {
healSquares1.push(new HealSquare(random((playerSize / 2), (width/2 - (playerSize / 2 + 5))), random((playerSize / 2) + hh1, (height - (playerSize / 2)))));
}
for(let i = healSquares1.length - 1; i >= 0; i–) {
healSquares1[i].show();
healSquares1[i].update();
if(healSquares1[i]) {
let d = dist(healSquares1[i].x, healSquares1[i].y, player.x, player.y);
if(d < (Math.sqrt(128) + Math.sqrt(512))) {
hw1 += 40
healSquares1.splice([i]);
}
}
}

This is really bugging me, and I’ve come so far with this game. It’s kinda my baby, and I just want to get it to where it’s totally playable. If anyone can help with this, and maybe show me where my logic is flawed, it would be very much appreciated. A reminder, I am working in p5, not in processing.

What IDE are you using to write your code? If you are using Processing in p5.js mode, you can press ctrl+t to nicely tabulate/indent your code before you paste it here. Then, you can format the code by selecting it and hitting the </> button before finishing posting your question.

Not sure if timer.count % 3000 is the best way to keep track of your time. Can you provide the code where timer.count is set and where it is increased?

Kf