Help with block break thingy

im trying to make a block break and regen thing. the code runs 2-4 times and then it never repeats again. i tried it with a while loop too and got the same result.

local isRegenerating = {}
runservice.Heartbeat:Connect(function()
	for i, plr in players:GetPlayers() do
		plr.CharacterAdded:Wait()
		local char = plr.Character
		task.wait()
		local BlockHP = char:GetAttribute("BlockHP")
		print("Loaded!")
		
		if BlockHP <= 0 then
			--Block break stuff here
			print("Block broken!")
			task.wait(5)
			print("Regenerated!")
			char.SetAttribute("BlockHP", 100)
			
		elseif BlockHP < 100 and not table.find(isRegenerating, char.Name) then
			table.insert(isRegenerating, char.Name)
			task.wait(2)
			print("Regenerated!")
			char.SetAttribute("BlockHP", 100) --Setting it back to full for testing
			task.wait()
			table.remove(isRegenerating, table.find(isRegenerating, char.Name))
			
		elseif BlockHP > 100 then
			char.SetAttribute("BlockHP", 100)
		end
	end
end)

There is no if statement checking if BlockHP is 100. The code checks if HP is lower than 0, lower than 100 or greater than 100. Try changing the code so it is checking if it is for example greater OR EQUAL to 100 by using >= operator. If you still don’t understand what I mean, run this code and look at your if statements in the code provided by you:

local BlockHP = 100
-- Let's simulate the statements in your code:
print(BlockHP <= 0) --> false
print(BlockHP < 100) --> false
print(BlockHP > 100) --> false
-- All of these statements are false; BlockHP is exactly 100
1 Like

oh thats not the issue mbmb its just that the loop runs for 1-4 times then dies

This prevents the code from running since the event doesn’t fire again until the player respawns. You need to check if the character exists before waiting for a new one:

local char = plr.Character or plr.CharacterAdded:Wait()
— only if the character doesn’t exist yet will the code wait for one
2 Likes

Hm, that’s weird. Try adding some print commands to the code to see which parts of the if statement are chosen to be run by the code.

1 Like

wow ye it worked thanksss!111!!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.