How to change for loop index?

Code:

local players = game:GetService("Players")

local function killHealth(player)
	local hv = 100
	local hum = player:FindFirstChildWhichIsA("Humanoid")
    
---i want the takeHealth for loop's value to return to 1 even after restoring 100 health
	for takeHealth = 1, 100 do
	    hv = hv - 1
		hum.Health = hv
		print("turd")
		wait(0.2)
		
		if hum.Health == 100 then
			hv = 100
			print("health restored")
			takeHealth = 1  --- my attempt at changing
		end
	end
end

local function charAdd(plr)
	plr.CharacterAdded:Connect(killHealth)
end

players.PlayerAdded:Connect(charAdd)

I want the for loop to return to the first index when the humanoid’s health is restored to 100 (there is another script that restores the health, unrelated)
so it can actually still take the entire health of the humanoid after restoring the health instead
of ending at index 100 everytime.

I’m assuming you want to reset the index? Correct me if I’m wrong.

Maybe create variables for the loop and change them accordingly (this is what I mean):

local var1 = 1
local var2 = 100

and the loop

for takeHealth = var1, var2 do

and when changing is needed

var1 = 1
var2 = 100

I am not entirely sure if this would work, but give it a try I guess.

Can’t you just have it on a ‘repeat until’ function that keeps removing health until it reaches 0?

local function killHealth(player)
	local hum = player:FindFirstChildWhichIsA("Humanoid")
    repeat
        hum.Health = hum.Health - 1
        wait(.2)
    until hum.Health <= 0
end
2 Likes

Why are you printing turd? Kinda odd ngl

1 Like