What do you want to achieve?
I’m trying to make an attribute value constantly go down if the value is higher than 0.
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.
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
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
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
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.