TweenService Error

Hello!

I am currently making a flexible Moving Platform that can be changed by the user with ease.

image
When I run the script, it shows this.

All of the things are setup correctly (Configuration changed for the test).

Please help!

Evidences:

-- General --
local TweenService = game:GetService("TweenService")

local Platform = script.Parent.MovingPlatform
local PositionsFolder = script.Parent.Positions

local TargetPosition = script.TargetPosition

-- Configuration --
local Duration = script.Parent.Configuration:GetAttribute("Duration")
local Positions = script.Parent.Configuration:GetAttribute("Positions")

--[[ Function ]]--

local TweeningInfo = TweenInfo.new(
	
	Duration,
	Enum.EasingStyle.Quart,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

while true do
	
	local NewPosition = PositionsFolder:FindFirstChild(TargetPosition.Value)

	local Info = {
		Position = Vector3.new(NewPosition.Position)
	}

	local Tween = TweenService:Create(Platform, TweeningInfo, Info)

	Tween:Play()
	TargetPosition.Value += 1

	wait(Duration)
	print("Successfully completed!")
	
	if TargetPosition.Value == Positions then
		
		TargetPosition.Value = 1
	end
	
	wait(1)
	
end

The Entire Script

image
Library of the model.

TargetPosition is a number value.
RunScript is the main script.

image
Properties of Configuration.

Thanks so much!

You’ll need to use CFrame to achieve your goal.

1 Like

Example:

game:GetService(“TweenService”):Create(Platform,TweenInfo.new(.35),{CFrame = TargetPosition.CFrame}):Play()

1 Like

Your issue is that you’re misusing FindFirstChild, make sure to tostring the number value.

local NewPosition = PositionsFolder:FindFirstChild(tostring(TargetPosition.Value))
Code structure tips

It may be cleaner for you to utilise Tween.Completed. For example: this code structure is considerably cleaner

local function tween()
  local t = TweenService:Create(…)
  
  t.Completed:Connect(function()
    tween()
  end)

  t:Play()
end)
1 Like

Some problems were already pointed out by others, but I believe the core issue might be the fact that TargetPosition.Value isn’t set to 1 when the script first runs, which leads to FindFirstChild() returning nil.

Let me know if this works:

-- General --
local TweenService = game:GetService("TweenService")

local Platform = script.Parent.MovingPlatform
local PositionsFolder = script.Parent.Positions

local TargetPosition = script.TargetPosition

-- Configuration --
local Duration = script.Parent.Configuration:GetAttribute("Duration")
local Positions = script.Parent.Configuration:GetAttribute("Positions")

--[[ Function ]]--

local TweeningInfo = TweenInfo.new(

	Duration,
	Enum.EasingStyle.Quart,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

TargetPosition.Value = 1

while true do

	local NewPosition = PositionsFolder:FindFirstChild(tostring(TargetPosition.Value))

	local Info = {
		Position = NewPosition.Position
	}

	local Tween = TweenService:Create(Platform, TweeningInfo, Info)

	Tween:Play()

	wait(Duration)
	print("Successfully completed!")

	if TargetPosition.Value == Positions then

		TargetPosition.Value = 1
	else

		TargetPosition.Value += 1
	end
	
	wait(1)

end

Also: If I understand the intention behind your configuration object, you are relying on the user entering the correct amount of positions as an attribute. You could also leave that to the script by getting the amount of position parts using #PositionsFolder:GetChildren(), thus avoiding an additional source of problems.

1 Like

Thank you so much! With some patches the entire thing works!

I’ll try to use that. Thanks for sharing!

I don’t understand much on how to use #PositionsFolder:GetChildren(), can you elaborate a bit more on how to use it?