Subtract removing more than 1 to a attribute

  1. What do you want to achieve?
    I’m trying to make an attribute value constantly go down if the value is higher than 0.
  2. What is the issue?
    The problem with that is that it starts going down to 99 then it goes to 97 and keeps going down. I just want to remove 1. To leave things clear, let’s say I want to subtract 1 to 10, the result will be 9 but with my script (since its a loop) it does that again just that the next time the value is for example 7, I just want to subtract only 1 until the value of the attribute is equal to 0.
  3. What solutions have you tried so far?
    I havent tried any solution because it just makes no sense at least for me that it does that.
while Folder:GetAttribute("Fatigue") > 1 do
	 wait(1.5)
        Folder:SetAttribute("Fatigue", (Folder:GetAttribute("Fatigue") - 1))
end

When using this statement:

Folder:GetAttribute("Fatigue") - 1

Try always using

Folder:GetAttribute("Fatigue") -= 1

or

Folder:GetAttribute("Fatigue") = Folder:GetAttribute("Fatigue") - 1

Side note; to clean up and simpliy code, you should create a function that would look somewhat like this:

local function SetAtt(AttName,  Value)
      Folder:SetAttribute(AttName,Value)
end

while Folder:GetAttribute("Fatigue") > 1 do
	 wait(1.5)
        SetAtt(Fatigue,Folder:GetAttribute("Fatigue") -= 1)
end

Tell me if this helps :slight_smile:

1 Like

wow thank you! however everytime I try using “-=” or “+=” it says “Expected ‘)’ (to close ‘(’ at column 10), got “-=””

That’s just a syntax error meaning that one of the functions is not closed off. Send your entire code if you’re still confused so I can fix it.

1 Like
local Player = game.Players.LocalPlayer
local Char = Player.Character
local Folder = Char:FindFirstChild("Stats")
Folder:SetAttribute("Fatigue", 0)
local Meter = script.Parent.Meter


Folder.AttributeChanged:Connect(function(attributeName)
	if attributeName == "Fatigue" then
		local NewSize = math.clamp((Folder:GetAttribute("Fatigue")/100),0,100)
		NewSize *= 0.4175
		Meter:TweenSize(UDim2.fromScale(NewSize, Meter.Size.Y.Scale), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.5, false, nil)
		local function SetAtt(AttName,  Value)
			Folder:SetAttribute(AttName,Value)
		end

		while Folder:GetAttribute("Fatigue") > 1 do
			wait(1.5)
			SetAtt("Fatigue",Folder:GetAttribute("Fatigue") - 1)
		end
	end
end)

The issue is that you cannot overwrite the returned value that a function gives.

local function SetAtt(AttName,  Value)
      Folder:SetAttribute(AttName,Value)
end

while Folder:GetAttribute("Fatigue") > 1 do
    wait(1.5)
    SetAtt("Fatigue", Folder:GetAttribute("Fatigue") - 1)
end

Fatigue was nil, because it was never declared.

1 Like

For the function:

--This
local function SetAtt(AttName,  Value)
	Folder:SetAttribute(AttName,Value)
end

On the outside of the Changed event.

I still dont understand the problem everything works my problem is that it starts working just fine and then it for some reason start’s removing more than 1 to the attribute value and it keeps doing that making that it starts removing more and more numbers, trying to make that a UI just slowly goes down but with this issue it goes down really quick.

It’s because you are using GetAttributeChangedSignal. Since you are changing the attribute value inside of the connection, the event is fired once again.

Oh, now I understand what’s the problem. Thank you @Towphy and @Jxl_s!