Tween only plays once

I made a script which tweens the orientation of a model. The issue is that the tween only runs once… Does anyone know why? The tween after the task.wait(10) never runs.

local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remotesFolder = ReplicatedStorage:WaitForChild("RemotesFolder")
local wheel1Event = remotesFolder:WaitForChild("Wheel1Event")

local Info = TweenInfo.new(2, Enum.EasingStyle.Linear)

local function onWheelActivation(PrimaryPart)
	
	local Goals = {CFrame = PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(180))}
	local tween = TweenService:Create(PrimaryPart, Info, Goals)
	
	tween:Play()
	task.wait(10)
	tween:Play()
end

wheel1Event.OnClientEvent:Connect(onWheelActivation)

Note: I’m forced to use CFrame for the Goals because Position only moves the PrimaryPart of the model. Every other part of the model doesn’t rotate when Position is used. I don’t know the reason for that.

1 Like

You could edit

local Info = TweenInfo.new(2, Enum.EasingStyle.Linear)

to

local Info = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1)

Which will have the code go back and forth

1 Like

Yeah, but I need it to stop for 10 seconds and then run again

You can either try to use task.delay or instantiate tween on call.

task.delay method:

local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remotesFolder = ReplicatedStorage:WaitForChild("RemotesFolder")
local wheel1Event = remotesFolder:WaitForChild("Wheel1Event")

local Info = TweenInfo.new(2, Enum.EasingStyle.Linear)

local function onWheelActivation(PrimaryPart)
    local Goals = {CFrame = PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(180))}
    local tween = TweenService:Create(PrimaryPart, Info, Goals)

    tween:Play()
    
    task.delay(10, function()
        tween:Play()
    end)
end

wheel1Event.OnClientEvent:Connect(onWheelActivation)

Instantiate on call (Create the tween anonymously and play it immediately):

local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remotesFolder = ReplicatedStorage:WaitForChild("RemotesFolder")
local wheel1Event = remotesFolder:WaitForChild("Wheel1Event")

local function playTween(PrimaryPart)
    TweenService:Create(
        PrimaryPart,
        TweenInfo.new(2, Enum.EasingStyle.Linear),
        {CFrame = PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(180))}
    ):Play()
end

local function onWheelActivation(PrimaryPart)
    playTween(PrimaryPart)
    task.wait(10)
    playTween(PrimaryPart)
end

wheel1Event.OnClientEvent:Connect(onWheelActivation)

I wrote the above scripts without a code editor and without debugging so let me know if you’re still having issues!

Hope this helps! :slight_smile:

1 Like

Your issue was not resetting the CFrame, so here is a modified code, to fix your issues!

Here is the fixed thing:

local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remotesFolder = ReplicatedStorage:WaitForChild("RemotesFolder")
local wheel1Event = remotesFolder:WaitForChild("Wheel1Event")

local Info = TweenInfo.new(2, Enum.EasingStyle.Linear)

local function onWheelActivation(PrimaryPart)
	local oldCFrame = PrimaryPart.CFrame
	local Goals = {CFrame = PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(180))}
	local tween = TweenService:Create(PrimaryPart, Info, Goals)

	tween:Play()
	task.wait(10)
	PrimaryPart.CFrame = oldCFrame
	tween:Play()
end

wheel1Event.OnClientEvent:Connect(onWheelActivation)

1 Like

It’s not playing because the tween’s goal has already been reached, if that makes sense. If it doesn’t make sense, I can try my best to elaborate, although it’s hard to explain.

Let’s say we have a kid named Conner. Conner is 4 years old and of course, as time goes by, he gets older, and as he gets older, he’s going to get taller. Let’s say that when Conner turns 16, he’ll be 6 feet tall, and he’ll no longer grow from that point. Of course, Conner will continue to get older as time goes by, but he will no longer grow because his maximum height is 6 feet.

Likewise, your tween is playing, but your tween’s goal has already been reached, so you cannot see the results of playing the same tween again. In order for your tween to play over again, you’ll need to update your tween’s goal or create a new tween. Here, for simplicity, I just updated the tween’s goal.

local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remotesFolder = ReplicatedStorage:WaitForChild("RemotesFolder")
local wheel1Event = remotesFolder:WaitForChild("Wheel1Event")

local Info = TweenInfo.new(2, Enum.EasingStyle.Linear)

local function onWheelActivation(PrimaryPart)
	local Goals = {CFrame = PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(180))}
	local tween = TweenService:Create(PrimaryPart, Info, Goals)

	tween:Play()
	task.wait(10)
	
	Goals.CFrame = PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(180))
	tween = TweenService:Create(PrimaryPart, Info, Goals)
	tween:Play()
end

wheel1Event.OnClientEvent:Connect(onWheelActivation)
2 Likes

The problem is probably that your second tween has the same goal, so when it starts playing it’s moving it to the position it’s already at. Instead, create two tweens, the second one with a goal of

PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(360))
1 Like

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