Hello Developers, I’ve created a Tool giver and integrated a timer, however, I encountered a bug which I believe is easy to fix, but I struggled to solve it. Would anyone be able to assist me?"
local lastnumber = 0
local Number = script.Parent.Number.Value
while task.wait(1) do
if script.Parent.Value.Value == true then
script.Parent.front.SurfaceGui.Frame.TextLabel.Text = "Time Left:"..Number..""
Number -= 1
elseif script.Parent.Value.Value == false then
script.Parent.front.SurfaceGui.Frame.TextLabel.Text = "Time Left:"..lastnumber..""
end
end
Giver Script:
local deb = false
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if deb then return end
local char = game.Players:GetPlayerFromCharacter(hit.Parent)
if char.Backpack:FindFirstChild("Unlucky -90%") then return end
game.ServerStorage.Clones:FindFirstChild("Unlucky -90%"):Clone().Parent = char.Backpack
deb = true
script.Parent.Parent.Value.Value = true
script.Parent.Parent.Number.Value = 10
wait(10)
deb = false
script.Parent.Parent.Number.Value = 0
script.Parent.Parent.Value.Value = false
end
end)
NO! You shouldn’t use .Value as a variable in your code!! it only stores the last number from the Number.Value and doesn’t constantly change it.
An example would be,
for your Script:
local lastnumber = 0
local Number = script.Parent.Number
while task.wait(1) do
if script.Parent.Value.Value == true then
script.Parent.front.SurfaceGui.Frame.TextLabel.Text = "Time Left:"..Number.Value..""
Number -= 1
elseif script.Parent.Value.Value == false then
script.Parent.front.SurfaceGui.Frame.TextLabel.Text = "Time Left:"..lastnumber..""
end
end
and you don’t need anything to change with your Giver Script.
If you want your script to be clean and not a spaghetti, try this like @MightyDantheman mentioned so.
And you only need your giver script assuming I know where your surfaceGui is:
local deb = false
local SurfaceGui = <the path to your surfacegui!>
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if deb then return end
local char = game.Players:GetPlayerFromCharacter(hit.Parent)
local Item = char.Backpack:FindFirstChild("Unlucky -90%")
if Item then return end
Item:Clone().Parent = char.Backpack
deb = true
for i = 10, 0, -1 do
SurfaceGui.Frame.TextLabel.Text = "Time Left: " .. i
end
deb = false
end
end)
Well also you don’t need the debounce code because ‘for’ loop yields!
For one this isn’t a “bug” per se, it’s just something you didn’t take into account when programming (personally dislike it when people use “bug” or “glitch” in this way).
And you can use math.clamp(YOURNUMBER, MIN, MAX), MIN with your minimum number (probably 0), and MAX is the maximum (100 or math.huge I guess, I don’t know your use case fully).