This problem is a bit difficult to explain but I’ll try. I am not very familiar with the task library btw so please be patient with me. I have a script where once your stamina reaches 100, the stamina bar will stay for 3 seconds before fading away. However, my problem was that if the player continued running after hitting 100 stamina the bar would still disappear. So I tried using Task.delay() on the disappearance so that if the player continued running I could use Task.cancel() to stop it from disappearing. I’m having a lot of trouble though. With my current code, i get the error, “invalid argument #1 to ‘cancel’ (thread expected, got nil)”.
Here’s my full code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(
1,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In
)
local info2 = TweenInfo.new(
3,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In
)
local Tween1 = TweenService:Create(script.Parent, info, {BackgroundTransparency = 0})
local Tween2 = TweenService:Create(script.Parent.Parent.StaminaBackground, info, {BackgroundTransparency = 0.6})
local Tween3 = TweenService:Create(script.Parent, info2, {BackgroundTransparency = 1})
local Tween4 = TweenService:Create(script.Parent.Parent.StaminaBackground, info2, {BackgroundTransparency = 1})
local DelayDisappearance
ReplicatedStorage.CanSprint.Stamina.Changed:Connect(function()
if script.Parent.BackgroundTransparency ~= 0 then
Tween1:Play()
Tween2:Play()
else
if ReplicatedStorage.CanSprint.Stamina.Value == 100 then
DelayDisappearance = task.delay(3, function()
if ReplicatedStorage.CanSprint.Stamina.Value == 100 then
Tween3:Play()
Tween4:Play()
end
end)
else
task.cancel(DelayDisappearance) -- Error is thrown here.
end
end
script.Parent.Size = UDim2.new(0, (190 / 100) * ReplicatedStorage.CanSprint.Stamina.Value, 0, 5)
end)
Please help!!