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
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