Help I’m trying to make a player shield system using SetAttribute() The other part of the script is working except I want to make the shield stop Recharge after it got completely broken for a set amount of time before resetting back to Max Shield and continue recharging
problem is it continue to recharge after the break is set to true until the 3-second wait finishes then it stops and never turns on ever again
My attempt so far
-- bunch of services
local player = game:GetService("Players")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local RunService = game:GetService("RunService")
local REGEN_RATE = 5/100 -- Recharge this fraction of Max Shield per second.
local REGEN_STEP = .1 -- Wait this long between each recharge step.
-- Attribute set up
Humanoid:SetAttribute("MaxShield", 100)
Humanoid:SetAttribute("Shield", 100)
Humanoid:SetAttribute("Break", false)
local MaxShield = Humanoid:GetAttribute("MaxShield")
local CurrentShield = Humanoid:GetAttribute("Shield")
local Break = Humanoid:GetAttribute("Break")
-- prevent Shield from go beyond Max Shield and not go negative
Humanoid:GetAttributeChangedSignal("Shield"):Connect(function()
if Humanoid:GetAttribute("Shield") < 0 then
Humanoid:SetAttribute("Shield", 0)
Humanoid:SetAttribute("Break", true) -- Shield break
end
if Humanoid:GetAttribute("Shield") > Humanoid:GetAttribute("MaxShield") then
Humanoid:SetAttribute("Shield", Humanoid:GetAttribute("MaxShield"))
Humanoid:SetAttribute("Break", false) -- Shield repair (I think)
end
CurrentShield = Humanoid:GetAttribute("Shield")
print(CurrentShield)
end)
local RegenShield = true
Humanoid:GetAttributeChangedSignal("Break"):Connect(function() -- check if player "break"
if Humanoid:GetAttribute("Break") == true then -- stop Shield regen (This is the part that has a problem)
RegenShield = false
wait(3)
Humanoid:SetAttribute("Break", false)
Humanoid:SetAttribute("Shield", MaxShield)
else
return
end
end)
while RegenShield == true do -- while loop for shield gen
while Humanoid:GetAttribute("Shield") < Humanoid:GetAttribute("MaxShield") do
local dt = wait(REGEN_STEP)
local dh = dt*REGEN_RATE*Humanoid:GetAttribute("MaxShield")
Humanoid:SetAttribute("Shield", math.min(Humanoid:GetAttribute("Shield") + dh, Humanoid:GetAttribute("MaxShield")))
end
Humanoid:GetAttributeChangedSignal("Shield"):wait()
end
Here is the file of the test place if you want to look at other thing
Shield Test.rbxl (66.7 KB)