Basically what im doing is changing the name of my tool every 0.1 seconds but sometimes it does 0.099999 (for a large amount of times) which gets annoying since it isnt visually appealing
I tried many solutions such as Clamping, Formatting using string.format and dividing then multiplying but it still doesnt want to work so uhh please help
local function Format(Number: number): string
local formatted = string.format("%.3f", Number)
formatted = formatted:gsub("%.?0+$", "")
return formatted
end
function ToolModule:Debounce(Tool: Tool, Time: number, Name: string)
assert(Tool, "Invalid Tool")
Time = Time or 2
assert(Name, "Invalid Name")
local Elapsed = 0
local Interval = 0.1
while task.wait(Interval) do
Tool.Name = Time - Elapsed
Elapsed += Format(Interval)
print(Elapsed)
if Elapsed >= Time then
Tool.Name = Name
break
end
end
end
^^
this is my current script
(i also tried using runservice with dt instead of a while loop but for some reason rounding up dt to a min of 0.001 will just not work ? idk, i gave up on that)
i actually did read your post and i tried it, yet it didnt work, so ill try again though if it does work ill be sure to mark as the solution (ill edit this message once i test it)
thanks!
Deleted the post because there’s a much better solution:
I don’t have the time at the moment to put it in your code but I’m sure you’re talented enough to! Here’s the code:
local function Format(Number: number): number
return math.floor(Number * 1000 + 0.5) / 1000
end
function ToolModule:Debounce(Tool: Tool, Time: number, Name: string)
assert(Tool, "Invalid Tool")
Time = Time or 2
assert(Name, "Invalid Name")
local Elapsed = 0
local Interval = 0.1
while task.wait(Interval) do
Elapsed = Format(Elapsed + Interval)
Tool.Name = tostring(Time - Elapsed)
print(Elapsed)
if Elapsed >= Time then
Tool.Name = Name
break
end
end
end
okay so i decided to remake my entire script for the debounce handling and i used a different approach, instead of incrementaly adding numbers which may cause floating point errors, i recalculate it each time and assign the value after that, and by using os.clock(), it reduces the risk even more so
heres my final script for the people who are interested in fixing a similar error!
function ToolModule:Debounce(Tool: Tool, Time: number, Name: string)
assert(Tool, "Invalid Tool")
Time = Time or 2
assert(Name, "Invalid Name")
local Elapsed = 0
local Interval = 0.1
local Start = os.clock()
Tool.Name = Time
while task.wait(Interval) do
local Elapsed = os.clock() - Start
local Remaining = math.max(Time - Elapsed, 0)
Remaining = math.floor(Remaining * 10 + 0.5) / 10
Tool.Name = Remaining
print(Elapsed)
if Elapsed >= Time then
Tool.Name = Name
break
end
end
end