Help with tweens, it will only play sometimes

Hello fellow developers!

I am making a script where when the player hovers over the part, it will rotate it and lift it up in the air, however, sometimes It will not work or only play the rotate tween.

I have added a task.wait() between the tween, which does fix it somewhat, as it will always play a tween, but now it will sometimes only play the move up tween. (script below)

Also, It will sometimes not play the tween out tween if the player mouse isn’t hovering over it anymore

I have tried to look online, but I cannot find any results similar to my problem

Any help will be appreciated!

local TweenService = game:GetService("TweenService")

local TweenInfoHTL = TweenInfo.new(0.75, Enum.EasingStyle.Back, Enum.EasingDirection.Out)
local TweenInfoHTD = TweenInfo.new(0.35, Enum.EasingStyle.Sine, Enum.EasingDirection.In)

task.wait(1)

for _, Part in workspace:GetDescendants() do
	if Part:IsA("Part") then
		if Part:FindFirstChild("ClickDetector") then
			local TweenInProgress = false
			
			local PreviousPositionY = Part.Position.Y
			local PreviousRotation = Part.Rotation
			
			local HoverTweenLevitate = TweenService:Create(Part, TweenInfoHTL, {Position = Vector3.new(Part.Position.X, PreviousPositionY + 1.75, Part.Position.Z)})
			local HoverTweenLevitateRotate = TweenService:Create(Part, TweenInfoHTL, {Rotation = Vector3.new(Part.Rotation.X + 34, Part.Rotation.Y + 24, Part.Rotation.Z + 12)})
			
			local HoverTweenDecend = TweenService:Create(Part, TweenInfoHTD, {Position = Vector3.new(Part.Position.X, PreviousPositionY, Part.Position.Z)})
			local HoverTweenDecendRotate = TweenService:Create(Part, TweenInfoHTD, {Rotation = PreviousRotation})

			local ClickDetector = Part:FindFirstChildWhichIsA("ClickDetector")

			ClickDetector.MouseHoverEnter:Connect(function()
				print("Entered")
				
				local SelectionBox = Instance.new("SelectionBox")
				SelectionBox.Color3 = Color3.fromRGB(255, 255, 255)
				SelectionBox.LineThickness = 0.1
				SelectionBox.Parent = Part
				SelectionBox.Adornee = Part
				
				TweenInProgress = true
				print("playing")
				task.wait()
				HoverTweenLevitateRotate:Play()
				task.wait()
				HoverTweenLevitate:Play()	
				
				HoverTweenLevitate.Completed:Wait()
				TweenInProgress = false
				print("played")
			end)
			
			ClickDetector.MouseHoverLeave:Connect(function()
				print("Removing")
				if TweenInProgress == true then
					HoverTweenLevitate:Cancel()
					HoverTweenLevitateRotate:Cancel()
				end
				
				if Part:FindFirstChild("SelectionBox") then
					Part.SelectionBox:Destroy()
				end
				
				TweenInProgress = true
				HoverTweenDecendRotate:Play()
				task.wait()
				HoverTweenDecend:Play()
				
				HoverTweenDecend.Completed:Wait()
				TweenInProgress = false
			end)
		end
	end
end
2 Likes

Don’t cancel the tweens, it glitches them because TweenService Tweens are intended to be called over one another.

3 Likes

Ah okay, so how can I stop the tween? If the player stops hovering over the brick, how do I make it so it can stop doing the hover(ed) tweens?

There can only be one tween running on an Instance at any given point in time. So if you play two tweens one after the other, only the second one will actually play (the first one will get auto-cancelled). So this is your issue:

HoverTweenDecendRotate:Play()
task.wait()
HoverTweenDecend:Play()

PS. Cancelling tweens is fine lol it works as expected idk what the other fella is on about

1 Like

Thanks so much! I have now fixed the issue by putting it all in one tween (like so):

local HoverTweenLevitate = TweenService:Create(Part, TweenInfoHTL, {Position = Vector3.new(Part.Position.X, PreviousPositionY + 1.75, Part.Position.Z), Rotation = Vector3.new(Part.Rotation.X + 34, Part.Rotation.Y + 24, Part.Rotation.Z + 12)})			
1 Like

image

local p = script.Parent
local TS = game:GetService("TweenService")

local t1 = TS:Create(
	p,
	TweenInfo.new(
		1,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.In
	),
	{
		Rotation = Vector3.new(0, 170, 0)
	}
)
local t2 = TS:Create(
	p,
	TweenInfo.new(
		1,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.In
	),
	{
		Rotation = Vector3.new(0, 0, 0)
	}
)

while task.wait(1) do
	t1:Play()
	task.wait(0.5)
	t1:Cancel()
	t2:Play()
end

Run this in Run mode and tell me what you see.
And then remove the Cancel().

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