My tween is tweening really fast

So theres a problem when selecting a button and selecting a different button, the tween starts to tween really fast. I tried to reset the tween but it still occurs.

local TweenService = game:GetService("TweenService")

local selectedTempUIStrokeColorTween
local previousSelectedTemp = nil

local defaultUIStrokeColor = Color3.fromRGB(131, 205, 255)

local function setSelectedTempUIStrokeColorTweenNil()
	if selectedTempUIStrokeColorTween then
		selectedTempUIStrokeColorTween:Pause()
		selectedTempUIStrokeColorTween = nil
	end
end

local function tweenSelectedTempUIStrokeColor()
	if selectedTemp ~= nil then
		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
		selectedTempUIStrokeColorTween = TweenService:Create(selectedTemp.UIStroke, tweenInfo, {Color = Color3.fromRGB(47, 255, 0)})
		selectedTempUIStrokeColorTween:Play()

		selectedTempUIStrokeColorTween.Completed:Connect(function()
			if selectedTemp ~= nil then
				local tweenInfo = TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
				selectedTempUIStrokeColorTween = TweenService:Create(selectedTemp.UIStroke, tweenInfo, {Color = defaultUIStrokeColor})
				selectedTempUIStrokeColorTween:Play()

				selectedTempUIStrokeColorTween.Completed:Connect(function()
					tweenSelectedTempUIStrokeColor()
				end)
			end
		end)
	end
end

local function connectButtonEvents(button)
	button.MouseButton1Click:Connect(function()
		if previousSelectedTemp then
			previousSelectedTemp.UIStroke.Color = defaultUIStrokeColor
		end

		if selectedTemp ~= button then
			previousSelectedTemp = selectedTemp
			selectedTemp = button
			
			setSelectedTempUIStrokeColorTweenNil()
			button.UIStroke.Color = defaultUIStrokeColor
			tweenSelectedTempUIStrokeColor()

			if previousSelectedTemp ~= nil then
				previousSelectedTemp.UIStroke.Color = defaultUIStrokeColor
			end
		else
			previousSelectedTemp = nil
			selectedTemp = nil

			setSelectedTempUIStrokeColorTweenNil()
			button.UIStroke.Color = defaultUIStrokeColor
		end
	end)
end

Video:

5 Likes

Hello! An easy way to change the speed of the tween is to change the time the tween is run. Currently, the tween speed is 0.6, I’d recommend 1 second to get started with.

2 Likes

Sorry, I meant that the tween for some reason plays really fast when selecting buttons.

I tried printing and it prints “finished” really fast:

local function tweenSelectedTempUIStrokeColor()
	if selectedTemp ~= nil then
		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
		selectedTempUIStrokeColorTween = TweenService:Create(selectedTemp.UIStroke, tweenInfo, {Color = Color3.fromRGB(47, 255, 0)})
		selectedTempUIStrokeColorTween:Play()

		selectedTempUIStrokeColorTween.Completed:Connect(function()
			if selectedTemp ~= nil then
				local tweenInfo = TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
				selectedTempUIStrokeColorTween = TweenService:Create(selectedTemp.UIStroke, tweenInfo, {Color = defaultUIStrokeColor})
				selectedTempUIStrokeColorTween:Play()

				selectedTempUIStrokeColorTween.Completed:Connect(function()
					tweenSelectedTempUIStrokeColor()
					print("finished") <--------------
				end)
			end
		end)
	end
end
1 Like

Sorry for the delay, but it may be because the tween finishes in 0.6 seconds, so it fires after 0.6 seconds have elapsed. An easy way to slow it down is to have a task.wait(extradelaynumber) in the .completed function.

2 Likes

That is not the problem, the problem occurs when you select a template and select a different template.

1 Like

Here is the place I created so you can test it.
Inventory Template.rbxl (53.7 KB)

2 Likes

I see what you mean, sorry I did not pick that up in the video above. The main issues were that .Completed wouldn’t fire until the tween was completed, and for some reason, wouldn’t cancel and be rewritten. So when a tween did complete after clicking multiple times, it would fire a few hundred times.

I sorta recoded a lot of it. If you have any questions, let me know!

Inventory Template.rbxl (53.7 KB)

3 Likes

Is there a more efficient way? I don’t like using loops.

2 Likes

Just tested it a little more, the only loop you can change is line 38

repeat task.wait() until SelectedTemp ~= CurrentlySelected

You can change it by using an object value. You would have to change a lot of stuff like SelectedTemp must have .Value after and the loop is shown above to a .changed. The efficiency with this, however, is very marginal and no one would notice.

1 Like

I found a better way of doing this:

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 defaultUIStrokeColor = Color3.fromRGB(0, 0, 0)
local tweenCache = {}

local function tweenSelectedTempUIStrokeColor(button)
	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()
		local tweenInfo = TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
		local tween = TweenService:Create(button.UIStroke, tweenInfo, {Color = defaultUIStrokeColor})
		tween:Play()

		tween.Completed:Connect(function()
			if selectedTemp == button then
				tweenSelectedTempUIStrokeColor(button)
			else
				tweenCache[button] = nil
			end
		end)
	end)

	tweenCache[button] = tween
	tween:Play()
end

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 setSelectedTempUIStrokeCacheNil()
	local prevTween = tweenCache[selectedTemp]

	if prevTween then
		prevTween:Cancel()
		tweenCache[selectedTemp] = nil
	end
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

				setSelectedTempUIStrokeCacheNil()
				tweenSelectedTempUIStrokeColor(v)
			else
				previousSelectedTemp = nil
				selectedTemp = nil
				resetPreviousSelectedTempTween()
			end
		end)
	end
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.