Please note, theres no errors, etc. Nothings broken per say, I just want to be able have the health ui stay a little longer when hitting the block. basically, if I hit the tree, UI should appear, and last 3 seconds. If I stop there, UI will disappear. If I keep hitting the tree however, it should continue staying there
Hit:FireServer(Model)
if Model.PrimaryPart:FindFirstChild('Status') then
Model.PrimaryPart.Status.Frame.Bar:TweenSize(UDim2.new(Model.Durability.Value / Model.HP.Value, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
else
local StatusClone = Status:Clone()
StatusClone.Parent = Model.PrimaryPart
StatusClone.Frame.Bar:TweenSize(UDim2.new(Model.Durability.Value / Model.HP.Value, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
Debris:AddItem(StatusClone, 3)
end
As you can see, it disappears, even tho Iām still clicking (this is because after the first click it stays for 3 seconds) Ineed it stay for 3 seconds up to the most recent click
Iām not trying to prevent the player from clicking multiple times. I already use debounce. Not sure what you are talking about ābreakā, nothings breaking. The UI is being destroyed.
Hit:FireServer(Model)
local db = false
function db()
if db = true then
wait(3)
db = false
end
end
db = true
db()
if Model.PrimaryPart:FindFirstChild('Status') then
Model.PrimaryPart.Status.Frame.Bar:TweenSize(UDim2.new(Model.Durability.Value / Model.HP.Value, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
else
local StatusClone = Status:Clone()
StatusClone.Parent = Model.PrimaryPart
StatusClone.Frame.Bar:TweenSize(UDim2.new(Model.Durability.Value / Model.HP.Value, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
if db = false then
Debris:AddItem(StatusClone, 0)
end
end
Hey, youāve never tried the actual script I sent, so you donāt know if it works or not. This is technically not a debounce for the breaking part of the tree, but the destroying of the UI
you need to reset the 3 second counter if you click again
Something like this
local t = 0
if Model.PrimaryPart:FindFirstChild('Status') then
Model.PrimaryPart.Status.Frame.Bar:TweenSize(UDim2.new(Model.Durability.Value / Model.HP.Value, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
t = 0
else
local StatusClone = Status:Clone()
StatusClone.Parent = Model.PrimaryPart
StatusClone.Frame.Bar:TweenSize(UDim2.new(Model.Durability.Value / Model.HP.Value, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
t = 0
while t < 3 do
t = t + wait(.05)
end
StatusClone:Destroy()
end
local Time = 0
if Part then
local Model = Part.Parent
if Model then
Hit:FireServer(Model)
if Model.PrimaryPart:FindFirstChild('Status') then
Model.PrimaryPart.Status.Frame.Bar:TweenSize(UDim2.new(Model.Durability.Value / Model.HP.Value, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
Time = 0
else
local StatusClone = Status:Clone()
StatusClone.Parent = Model.PrimaryPart
StatusClone.Frame.Bar:TweenSize(UDim2.new(Model.Durability.Value / Model.HP.Value, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
Time = 0
while Time < 3 do
Time = Time + wait(0.05)
end
StatusClone:Destroy()
end
end
end
The bar doesnāt move. It stays static, until the frame gets destroyed. And even if I continue clicking, it still gets deleted after 3 seconds (not 3 seconds after last click)
Everytime you click the tree, reset a timer value to 0 and just keep setting that value to tick(). If tick()-value is greater than or equal to 3, then destroy the gui, if it exists. (edited oopsie)
oops, you need to put the while loop in a spawn() function like so
spawn(function()
while t > 3 do
t = t + wait(.05)
end
end)
I kinda misunderstood how you were doing it. I suggest you :FireServer(), update the values on the server, and then :FireClient() to the player who :FireServer()'d. OnClientEvent should handle the guis. This should stop you from having to create another thread in the script
local counter = 0
local debounce = false
local function StartCounting()
counter = 3
-- wrap next bit in debounce so itās running a maximum of one time concurrently
while counter > 0 do
wait(1)
counter = counter - 1
end
GUI:Destroy()
end
Call the function every time the action happens. The count variable will keep getting reset to 3. The while loop runs infinitely until the counter hits 0, at which point the block ends and the gui is destroyed.
Still not 100% working. The UI disappears and then quickly reappears.
As for remove event returning to client, I dont wanna do that, as usually theres a whole second, sometimes longer between it coming back to the client. I want instant.
I faced the similar issue recently.
You may need to touch the script a bit deeper.
My workaround in 2 steps:
1)
add a numeric āLifeTimeā value to Status object, with default value 3 (sec)
add a āself-destructionā script to Status object, that monitors if the time since the object spawn (in tick()) elapsed more than the value stored in LifeTime. If yes, destroy the object.
when you clone the Status object, enable the script.
If nothing happens after cloning the object (as described above), the object will be destroyed after 3 sec.
when you hit the Model (tree) for the first time, clone the Status object and save the tick() to a variable (e.g. ātā)
whenever you hit the Model (tree) again during the lifetime of the cloned Status object, add the difference of the current tick() and the saved value (ātā) to the Status clone āLifeTimeā value. It will keep your Status bar lasts exactly as long as you wished.
It should work.
(I canāt write and test a code right now, but I solved it in this way.)
Hit:InvokeServer(Model)
local Time = tick()
MostRecentCall = Time
if Model.PrimaryPart:FindFirstChild('Status') then
if Model and Model.Parent then
Model.PrimaryPart.Status.Frame.Bar:TweenSize(UDim2.new(Model.Durability.Value / Model.HP.Value, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
end
else
local StatusClone = Status:Clone()
StatusClone.Parent = Model.PrimaryPart
StatusClone.Frame.Bar.Size = UDim2.new(Model.Durability.Value / Model.HP.Value, 0, 1, 0)
spawn(function()
wait(3)
if Time == MostRecentCall then
StatusClone:Destroy()
end
end)
end
Got it so close. It doesnāt disappear when I keep clicking, however, it just doesnāt disappear at all now
I used a Part with a ClickDetector + child SurfaceGui/TextLabel, but the logic is the same as your Model / Statusbar.
lifeTime is a NumberValue object, child of the TextLabel (default value can be any).
Make SurfaceGui Disabled by default.
Hit script (fires when you click on the part):
part = script.Parent.Parent
label = part.SurfaceGui.TextLabel
local t
part.ClickDetector.MouseClick:Connect(function()
if not part.SurfaceGui.Enabled then
part.SurfaceGui.Enabled = true
label.lifeTime.Value = 3
label.selfdestructScript.Disabled = false
t = tick()
else
label.lifeTime.Value = label.lifeTime.Value + tick() - t
t = tick()
end
end)
Self-destruction script (a child of the TextLabel, make it Disabled by default)
local spawnTime = tick()
while tick()-spawnTime < script.Parent.lifeTime.Value do
wait()
end
script.Parent.Parent.Enabled = false
script.Disabled = true