Parts not oscillating

I’ve made a model with a primary part in the center of the model. Around the primary part, there are several regular parts. These regular parts slowly oscillate and move away from the primary part and then move back in their original position relative to the primary part, the oscillation of moving back is exactly the same way as moving away. But the problem is that, when I try to move the parts back, instead of being slowly oscillated, it’s instant. Can someone help me with this? This is my code:

local RunService = game:GetService("RunService")

local Model = script.Parent
local PrimaryPart = Model.PrimaryPart

local PartInfo = {}

for _, part in pairs(Model:GetChildren()) do
	if part:IsA("BasePart") and part ~= PrimaryPart and part.Name ~= "Halo" then
		local originalPosition = PrimaryPart.CFrame:pointToObjectSpace(part.Position)
		
		PartInfo[part] = {
			InitialPosition = originalPosition,
			IsMovingAway = true,
			StartTime = tick(),
			AnimationDuration = 5
		}
	end
end

local function smoothInOut(t)
	return 0.5 - 0.5 * math.cos(math.pi * t)
end

RunService.Stepped:Connect(function(_, Delta)
	for part, data in pairs(PartInfo) do
		local elapsed = tick() - data.StartTime
		local t = (elapsed % data.AnimationDuration) / data.AnimationDuration
		local phase = smoothInOut(t)

		local newOffset
		
		if data.IsMovingAway then
			newOffset = data.InitialPosition * phase
		else
			newOffset = data.InitialPosition * (1 - phase)
		end

		part.Position = PrimaryPart.CFrame:pointToWorldSpace(data.InitialPosition + newOffset)

		if t >= 1 then
			data.IsMovingAway = not data.IsMovingAway
			data.StartTime = tick()
		end
	end
end)
1 Like

I have no idea what the issue is, but perhaps using TweenService for this could solve it. TweenService also comes with the boolean toggle to reverse once played.

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