So I have a problem that the tween starts to play really fast when pressing the buttons fast.
I have tried setting a coroutine connection to disconnect but that didn’t work.
The tweenCache doesn’t solve the problem.
I also tried to disconnect the tween completed function but that didn’t work either.
local TweenService = game:GetService("TweenService")
local MainFrame = script.Parent
local Frame = MainFrame:WaitForChild("Frame")
local List = Frame:WaitForChild("List")
local selectedTemp = nil
local previousSelectedTemp = nil
local tweenConnection = nil
local defaultUIStrokeColor = Color3.fromRGB(0, 0, 0)
local tweenCache = {}
local function resetPreviousSelectedTempTween()
if previousSelectedTemp then
local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
local tween = TweenService:Create(previousSelectedTemp.UIStroke, tweenInfo, {Color = defaultUIStrokeColor})
tween:Play()
end
end
local function stopAllUIStrokeCacheNil()
for button, tween in pairs(tweenCache) do
tween:Cancel()
tweenCache[button] = nil
end
end
local function setTweenConnectionNil()
if tweenConnection then
tweenConnection:Pause()
tweenConnection = nil
end
end
local function tweenSelectedTempUIStrokeColor(button)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0)
tweenConnection = TweenService:Create(button.UIStroke, tweenInfo, {Color = Color3.fromRGB(47, 255, 0)})
tweenConnection.Completed:Connect(function()
local tweenInfo = TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
tweenConnection = TweenService:Create(button.UIStroke, tweenInfo, {Color = defaultUIStrokeColor})
tweenCache[button] = tweenConnection
tweenConnection:Play()
tweenConnection.Completed:Connect(function()
if selectedTemp == button then
tweenSelectedTempUIStrokeColor(button)
else
print("nil")
tweenCache[button] = nil
setTweenConnectionNil()
stopAllUIStrokeCacheNil()
end
end)
end)
tweenCache[button] = tweenConnection
tweenConnection:Play()
end
for i,v in ipairs(List:GetChildren()) do
if v:IsA("ImageButton") then
v.MouseButton1Click:Connect(function()
if selectedTemp ~= v then
resetPreviousSelectedTempTween()
previousSelectedTemp = selectedTemp
selectedTemp = v
setTweenConnectionNil()
stopAllUIStrokeCacheNil()
tweenSelectedTempUIStrokeColor(v)
else
previousSelectedTemp = nil
selectedTemp = nil
setTweenConnectionNil()
stopAllUIStrokeCacheNil()
resetPreviousSelectedTempTween()
end
end)
end
end
local TweenService = game:GetService("TweenService")
local MainFrame = script.Parent
local Frame = MainFrame:WaitForChild("Frame")
local List = Frame:WaitForChild("List")
local defaultUIStrokeColor = Color3.fromRGB(0, 0, 0)
local wasALastButton = false
local lastButton = nil
local tween = nil
for i,v in List:GetChildren() do
if v:IsA("ImageButton") then
v.MouseButton1Click:Connect(function()
local function loop()
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0)
tween = TweenService:Create(v.UIStroke, tweenInfo, {Color = Color3.fromRGB(47, 255, 0)})
tween:Play()
tween.Completed:Once(function(state)
if state == Enum.PlaybackState.Cancelled then return end
tweenInfo = TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
tween = TweenService:Create(v.UIStroke, tweenInfo, {Color = defaultUIStrokeColor})
tween:Play()
tween.Completed:Once(function()
if state == Enum.PlaybackState.Cancelled then return end
loop()
end)
end)
end
if wasALastButton then
tween:Cancel()
lastButton.UIStroke.Color = Color3.fromRGB(defaultUIStrokeColor)
if lastButton == v then
lastButton = nil
tween = nil
wasALastButton = false
return
end
else
wasALastButton = true
end
lastButton = v
loop()
end)
end
end
local TweenService = game:GetService("TweenService")
local MainFrame = script.Parent
local Frame = MainFrame:WaitForChild("Frame")
local List = Frame:WaitForChild("List")
local defaultUIStrokeColor = Color3.fromRGB(0, 0, 0)
local tweenATweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0)
local tweenBTweenInfo = TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
local lastButton = nil
local tween = nil
for i,v in List:GetChildren() do
if v:IsA("ImageButton") then
v.MouseButton1Click:Connect(function()
local function loop()
local tweenA = TweenService:Create(v.UIStroke, tweenATweenInfo, {Color = Color3.fromRGB(47, 255, 0)})
tweenA:Play()
tweenA.Completed:Once(function(state)
if state == Enum.PlaybackState.Cancelled then return end
local tweenB = TweenService:Create(v.UIStroke, tweenBTweenInfo, {Color = defaultUIStrokeColor})
tweenB:Play()
tweenB.Completed:Once(function(state)
if state == Enum.PlaybackState.Cancelled then return end
loop()
end)
tween = tweenB
end)
tween = tweenA
end
if lastButton == v then
tween:Cancel()
lastButton.UIStroke.Color = defaultUIStrokeColor
lastButton = nil
elseif lastButton then
tween:Cancel()
lastButton.UIStroke.Color = defaultUIStrokeColor
loop()
lastButton = v
else
loop()
lastButton = v
end
end)
end
end
if previousSelectedTemp then
resetPreviousSelectedTempTween()
if previousSelectedTemp == button then
previousSelectedTemp = nil
selectedTemp = nil
else
previousSelectedTemp = selectedTemp
selectedTemp = button
tweenSelectedTempUIStrokeColor(button)
end
resetPreviousSelectedTempTween()
end
resetPreviousSelectedTempTween()
previousSelectedTemp = button
tweenSelectedTempUIStrokeColor(button)
Full code:
local selectedTemp = nil
local previousSelectedTemp = nil
local currentTween = nil
local defaultUIStrokeColor = Color3.fromRGB(131, 205, 255)
local function resetPreviousSelectedTempTween()
if previousSelectedTemp then
local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
local tween = TweenService:Create(previousSelectedTemp.UIStroke, tweenInfo, {Color = defaultUIStrokeColor})
tween:Play()
end
end
local function tweenSelectedTempUIStrokeColor(button)
if currentTween and currentTween.PlaybackState == Enum.PlaybackState.Playing then
currentTween:Cancel()
end
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0)
local tween = TweenService:Create(button.UIStroke, tweenInfo, {Color = Color3.fromRGB(47, 255, 0)})
tween.Completed:Connect(function(state)
if state == Enum.PlaybackState.Completed then
tweenInfo = TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
local resetTween = TweenService:Create(button.UIStroke, tweenInfo, {Color = defaultUIStrokeColor})
resetTween:Play()
resetTween.Completed:Connect(function(state)
if state == Enum.PlaybackState.Completed then
tweenSelectedTempUIStrokeColor(button)
end
end)
end
end)
currentTween = tween
tween:Play()
end
local function connectButtonEvents(button)
button.MouseButton1Click:Connect(function()
if previousSelectedTemp then
resetPreviousSelectedTempTween()
if previousSelectedTemp == button then
previousSelectedTemp = nil
selectedTemp = nil
else
previousSelectedTemp = selectedTemp
selectedTemp = button
tweenSelectedTempUIStrokeColor(button)
end
resetPreviousSelectedTempTween()
end
resetPreviousSelectedTempTween()
previousSelectedTemp = button
tweenSelectedTempUIStrokeColor(button)
end)
end
local selectedTemp = nil
local previousSelectedTemp = nil
local currentTween = nil
local tweenCache = {}
local defaultUIStrokeColor = Color3.fromRGB(131, 205, 255)
local function resetPreviousSelectedTempTween()
if previousSelectedTemp then
local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
local tween = TweenService:Create(previousSelectedTemp.UIStroke, tweenInfo, {Color = defaultUIStrokeColor})
tween:Play()
end
end
local function tweenSelectedTempUIStrokeColor(button)
if currentTween and currentTween.PlaybackState == Enum.PlaybackState.Playing then
currentTween:Cancel()
end
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0)
local tween = TweenService:Create(button.UIStroke, tweenInfo, {Color = Color3.fromRGB(47, 255, 0)})
tween.Completed:Connect(function(state)
if state == Enum.PlaybackState.Completed then
tweenInfo = TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
local resetTween = TweenService:Create(button.UIStroke, tweenInfo, {Color = defaultUIStrokeColor})
resetTween:Play()
resetTween.Completed:Connect(function(state)
if state == Enum.PlaybackState.Completed then
tweenSelectedTempUIStrokeColor(button)
end
end)
end
end)
currentTween = tween
tween:Play()
end
local function tweenPetImageUIScale()
local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0)
local tween = TweenService:Create(PetInfoFrame.PetImage.UIScale, tweenInfo, {Scale = 1})
tween:Play()
end
local function setPetInfoFrame()
if selectedTemp ~= nil then
local petData = PetsData.GetPetData(selectedTemp:GetAttribute("ID"))
if petData then
PetInfoFrame.PetNameLabel.Text = petData.PetName
PetInfoFrame.PetImage.Image = petData.PetImage
local isEquipped = checkPetEquipped(selectedTemp.Name)
if isEquipped then
EquipButton.BackgroundColor3 = Color3.fromRGB(214, 0, 0)
EquipButton.Text = "Unequip"
else
EquipButton.BackgroundColor3 = Color3.fromRGB(41, 220, 22)
EquipButton.Text = "Equip"
end
end
end
end
local function connectButtonEvents(button)
button.MouseButton1Click:Connect(function()
local UIScale = button:FindFirstChildWhichIsA("UIScale")
if UIScale then
tweenUIScaleClick(UIScale, 0.6, 1)
end
if not isMultiDeleting then
if tick() - selectTempCurrentTime >= selectTempCooldown then
selectTempCurrentTime = tick()
if previousSelectedTemp then
resetPreviousSelectedTempTween()
if previousSelectedTemp == button then
previousSelectedTemp = nil
selectedTemp = nil
tweenListSizeClose()
tweenClosePetInfoFrame()
resetPetInfoFrame()
else
previousSelectedTemp = selectedTemp
selectedTemp = button
PetInfoFrame.Visible = true
setPetInfoFrame()
tweenListSizeOpen()
tweenOpenPetInfoFrame()
PetInfoFrame.PetImage.UIScale.Scale = 0
tweenPetImageUIScale()
tweenSelectedTempUIStrokeColor(button)
end
resetPreviousSelectedTempTween()
end
resetPreviousSelectedTempTween()
previousSelectedTemp = button
tweenSelectedTempUIStrokeColor(button)
--[[
if selectedTemp ~= button then
resetPreviousSelectedTempTween()
previousSelectedTemp = selectedTemp
selectedTemp = button
PetInfoFrame.Visible = true
setPetInfoFrame()
tweenListSizeOpen()
tweenOpenPetInfoFrame()
PetInfoFrame.PetImage.UIScale.Scale = 0
tweenPetImageUIScale()
tweenSelectedTempUIStrokeColor(button)
else
previousSelectedTemp = nil
selectedTemp = nil
tweenListSizeClose()
tweenClosePetInfoFrame()
resetPetInfoFrame()
resetPreviousSelectedTempTween()
end
]]
end
else
if not table.find(multiDeletedPets, button.Name) then
table.insert(multiDeletedPets, button.Name)
button.DeleteLabel.Visible = true
else
table.remove(multiDeletedPets, table.find(multiDeletedPets, button.Name))
button.DeleteLabel.Visible = false
end
end
end)
end