I have a issue. I my game I have a Gui that show’s how much damage you dealt, so a damage counter. The problem is, I want it to be a non-float number but naturally some abilities won’t always deal a full integer number. But the issue is, if I use math.round on the text the damage will be inaccurate because it uses the previous text to make the new text since it add the extra damage to the previous text. (Sorry that was a mouthful)
Here is my code for context (its a modulescript):
local DamageCounterModule = {}
local TweenService = game:GetService("TweenService")
local currentCoroutine = nil
local damageLabel = script.Parent
local damageLabelColorTweenInfo = TweenInfo.new(2,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
local damageLabelColorTween = nil
local damageLabelTransparencyTweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
local damageLabelTransparencyTween = TweenService:Create(damageLabel,damageLabelTransparencyTweenInfo,{TextTransparency = 1})
function DamageCounterModule.AddDamage(damage)
if tonumber(damage) == nil then warn("Damage wasn't a number. Please use numbers when adding to damage.") return end
if currentCoroutine ~= nil then
task.cancel(currentCoroutine)
end
print("Adding Damage To Counter:",damage)
damageLabelColorTween = TweenService:Create(damageLabel,damageLabelColorTweenInfo,{TextColor3 = Color3.fromRGB(0,0,0)})
damageLabel.TextColor3 = Color3.fromRGB(255, 85, 0)
damageLabel.TextTransparency = 0
damageLabel.Visible = true
damageLabel.Text = tonumber(damageLabel.Text) + damage
damageLabelColorTween:Play()
currentCoroutine = task.delay(8,function()
damageLabelTransparencyTween:Play()
damageLabelTransparencyTween.Completed:Wait()
damageLabel.Visible = false
damageLabel.Text = 0
end)
end
return DamageCounterModule
So, what how can the wonderful forum help me this time? (Also don’t just tell me to separate the actual value and the text into separate variables I know that is possible.)
But would that change the value still, or just hide the decimals? Because the issue is is that I need the text to have no decimals but when adding the damage it still has the og number (with decimals).
And I’m also trying not to use another variable.
Sounds like you should be working with two values: the value of the damage being applied (this is a number with exact precision), and the value to be displayed as damage (this is a string formatted to be rounded to the desired precision). In the following code, assume the exactDamage variable is the exact precision of damage applied and the formattedDamage is the string that will be displayed to the user:
local exactDamage: number = 23 / 7 --example number that has repeating decimal points
print(exactDamage) --> 17.571428571428573
local formattedDamage: string = string.format("%.1f", exactDamage)
print(formattedDamage) --> "17.6"
print(exactDamage) --> 17.571428571428573 --the value of `exactDamage` remains unchanged
You’ll want to store an internal number that’s not rounded and then display the rounded number in the text. Storing the numerical value inside a label’s text isn’t great practice for this same reason: the visual representation of the number can have rounding, symbols ($), etc that get in the way of storing the number.
One easy way you could do this is store the actual damage value inside an attribute:
-- Ex: Set the damage:
damageLabel:SetAttribute("Damage", newDamage)
-- Ex: Get the damage:
damageLabel:GetAttribute("Damage")
-- Ex: Update the label and damage:
local oldDamage = damageLabel:GetAttribute("Damage") -- Get the internal value
local newDamage = oldDamage + damage -- Calculate the new value
damageLabel.Text = string.format("%.1f", newDamage) -- Update the label with a rounded number
damageLabel:SetAttribute("Damage", newDamage) -- Update the internal number
One cool pattern you can use is AttributeChanged for auto updates: you can make it so when you update the “Damage” attribute it automatically updates the text with a rounded version:
damageLabel.AttributeChanged:Connect(function(attributeName)
-- If the "Damage" attribute was updated then update the text label with a rounded number
if attributeName == "Damage" then
damageLabel.Text = string.format("%.1f", damageLabel:GetAttribute("Damage"))
end
end)
-- Then you can just update the "Damage" whenever and have the text automatically update:
damageLabel:SetAttribute("Damage", newDamage) -- Triggers an update