Hello, I am trying to make a script that will change the health of an NPC based on the number times it respawns. Ive got a respawn script to work, however, i am trying to get a script to count its deaths. (Note: I havent tried to change the health yet until i can get a number for the deaths.) Below is my script so far...If i made a simple mistake or something stupid, please dont be afraid to call me stupid because i want to learn from my mistakes. Thank you so much!
1 | local human = game.Workspace.testfighter.Humanoid |
2 |
3 | local count = human.Died.Value |
4 |
5 | while human.Died:Wait(count) do |
6 | count = count + 1 |
7 | print (count) |
8 | end |
That is not proper use of a while loop. What I believe you're trying to do is quite the same as an event-connected function so attach it to a function instead of :Wait() since you're attempting to get every single time the NPC dies. Also you can use a number instead of creating a unnecessary instance to record your numbers as variables can hold numbers.
1 | local human = game.Workspace.testfighter.Humanoid |
2 |
3 | local count = 0 -- starter count |
4 |
5 | human.Died:Connect( function () -- event tied to function |
6 | count = count + 1 -- keeps increasing at each death |
7 | print (count) |
8 | end ) |
This will track each time the NPC dies and runs the code within the function each of those times. It will also count numbers more efficiently then using a NumberValue or IntValue since those are created objects.