[Request] 1HP every 2Seconds

Started by Gulk, Apr 29, 2015, 05:15 PM

Previous topic - Next topic

Gulk

Can someone make a script that heals only a certain SkinID +1 HP every 2 seconds?
(I need it for Sailor skin in my server)

Thanks

karan20000000000

You may try this out (untested) :
function onScriptLoad()
{
  NewTimer("healer",2000,0);
}

function healer()
{
  for(local i=0;i<GetMaxPlayers();i++)
  {
    local plr = FindPlayer(i);
    if(plr && (plr.Skin == 134 || plr.Skin == 135 || plr.Skin==136))
    {
      if(plr.Health<100) plr.Health+=1;
    }
  }
}
With regards,
KP
------------------------------------------

.

#2
Quote from: karan20000000000 on Apr 29, 2015, 05:29 PMYou may try this out (untested) :
...

Cache that GetMaxPlayers() function to reduce the number of function calls. And use pre-increment '++i' instead of post-decrement 'i++' to avoid a copy of that variable on each loop.

local maxp = GetMaxPlayers();
for(local i=0;i<maxp;++i)
// ....

While were at it, you could also turn that into a reversed do/while loop as and save a couple more instructions as well:
local i = GetMaxPlayers();
do {
    // your code...
} while (--i >= 0)

Resulting:
local i = GetMaxPlayers();
do {
    local plr = FindPlayer(i);
    if(plr && (plr.Skin == 134 || plr.Skin == 135 || plr.Skin==136))
    {
      if(plr.Health<100) plr.Health+=1;
    }
} while (--i >= 0)
.

Gulk

Wow, it works. thanks both of you.
I used S.L.C's because as he explained it is more efficient

Sebastian

There is a function called GetPlayers() that returns the nr of online players, so you should use that one instead of GetMaxPlayers().

Gulk

#5
How can I stop the healing when HP reaches 0 before the death is acknowledged? its causing a visual bug on my spawnscreen (the bug doesnt matter that much since its not game breaking) I'm just curious to how ;p I've made some fail attempts

Shadow

Quote from: sseebbyy on Apr 29, 2015, 07:12 PMThere is a function called GetPlayers() that returns the nr of online players, so you should use that one instead of GetMaxPlayers().

Not relevant. You can have 3 players with IDs 5, 4, and 6 which means the function will do nothing.
QuotePS:is trash is ur home language??

.

#7
@sseebbyy Yeah but it's useless. Let me put that in plain therms by having a fictional story:

Jimmi's server is now online and 5 players are connected:
  • [ID:0] FunKYBoy
  • [ID:1] YoMaMa
  • [ID:2] PervertedFk
  • [ID:3] IMOUTOFNAMES
  • [ID:4] KingOfNOTHING

Cool, you can see their ID's on the left. Now let's call GetPlayers(). That should return 5. Nice. Let's walk through the process of printing their names:

local players GetPlayers();

for (local i = 0; i < players; ++i)
{
    local plr = FindPlayer(i);
    if (plr) print("Found Player at pos " + i + " with name " + plr.Name);
}

Since GetPlayers() returned 5 and we tested for 0 through 4 because we start from 0. This should look like:

Found Player at pos 0 with name FunKYBoy
Found Player at pos 1 with name YoMaMa
Found Player at pos 2 with name PervertedFk
Found Player at pos 3 with name IMOUTOFNAMES
Found Player at pos 4 with name KingOfNOTHING

Now, PervertedFk and IMOUTOFNAMES got bored and leave. Let's see how that looks:
  • [ID:0] FunKYBoy
  • [ID:1] YoMaMa
  • [ID:2]
  • [ID:3]
  • [ID:4] KingOfNOTHING

Now let's call GetPlayers() again. Obviously that returns 3. So, let's do that again.

local players GetPlayers();

for (local i = 0; i < players; ++i)
{
    local plr = FindPlayer(i);
    if (plr) print("Found Player at pos " + i + " with name " + plr.Name);
}

That should output:

Found Player at pos 0 with name FunKYBoy
Found Player at pos 1 with name YoMaMa

But wait, where's KingOfNOTHING? Well, don't you remember that GetPlayers() returned 3 because there's 3 players on the server? And since we iterated 3 positions starting from 0, we didn't even got to 4.


I hope that explains the issue with idle slots and why that's useless. Would be better if that function returned an array with integers containing the ID's of connected players.
.

.

Quote from: Gulk on Apr 29, 2015, 07:18 PMHow can I stop the healing when HP reaches 0 before the death is acknowledged? its causing a visual bug on my spawnscreen (the bug doesnt matter that much since its not game breaking) I'm just curious to how ;p I've made some fail attempts

I'm sure you already tested for 0 HP. Anyway, I haven't tested this and it's probably wrong because the function might get called immediately after the onPlayerDeath() event and therefore it wouldn't even know that player ever reached 0. Which means the function just adds 1 HP continuously because of the large time interval it might be hard to catch that break moment when the player reaches 0 HP.

local i = GetMaxPlayers();

do {
    local plr = FindPlayer(i);
    if (!plr) continue;

    switch (plr.Skin)
    {
        case 134:
        case 135:
        case 136:
            if (plr.Health < 100 && plr.Health > 0) ++plr.Health;
        break;
    }
} while (--i >= 0)
.

Gulk

that works perfect, i set the minimum healing hp to 11, so 10hp or below and no hp regen. you're dead meat (just like mta 0.5)

many thanks

Gudio

I'd use a table that keeps players with Sailor skins.

Maddy

Just saying, in XAD, whenever we try to capture the CP, our HP increases, maybe Gudio can help with that.