Need help on moving Models Using Tween Service

So I was following a YouTube tutorial about moving a model using TweenService because I wanted to make the set of buildings move so I could make an illusion that the bus is moving. I just came back scripting from a long time so I must have missed or overlooked something but please let me know.

I made sure that I followed what I need to follow in the video but for some reason
I was given this error:

Workspace.SpawnBus.CityBuildings.MoveScript:10: attempt to index nil with 'PrimaryPart'

The model “BuildingSet1” already has a primary part, so I don’t know what is nil.

My Script:

local BuildingSet1 = script.Parent:WaitForChild("BuildingSet1")
local BuildingSet2 = script.Parent:WaitForChild("BuildingSet2")
local TweenService = game:GetService("TweenService")

local function MoveSet1(BuildingSet1)
	local TweenInfo1 = TweenInfo.new(5, Enum.EasingStyle.Sine,Enum.EasingDirection.InOut, -1)
	local Cframe1 = Instance.new("CFrameValue")
	Cframe1.Value = BuildingSet1:GetPrimaryPartCFrame() -- Where I got the error
	Cframe1:GetPropertyChangedSignal("Value"):Connect(function()
		BuildingSet1:GetPrimaryPartCFrame(Cframe1.Value)
	end)
	local Tween1 = TweenService:Create(Cframe1,TweenInfo1, {Value = CFrame.new(-1053.61, 11.9, -839.46)})
	Tween1:Play()
	wait(1)
end

MoveSet1()

I have solved a similar case in the past, but it has nothing to do with tweening. Here you see the conversation: Tweening in a circle

2 Likes

It’s a good idea but the problem is when I run it it causes the game to lag a lot and it’s harder to change the position smoother, which is why I prefer using tween service because it is able to make a smooth movement.

anyways I was able to solve it:

new script:

local BuildingSet1 = script.Parent:WaitForChild("BuildingSet1")
local BuildingSet2 = script.Parent:WaitForChild("BuildingSet2")
local TweenService = game:GetService("TweenService")

print(workspace.SpawnBus.CityBuildings.BuildingSet1.PrimaryPart.CFrame)

local function MoveSet1(BuildingSet1)
	local TweenInfo1 = TweenInfo.new(5, Enum.EasingStyle.Sine,Enum.EasingDirection.InOut, -1)
	local Cframe1 = Instance.new("CFrameValue")
	Cframe1.Value = workspace.SpawnBus.CityBuildings.BuildingSet1.PrimaryPart.CFrame -- Where I got the error
	Cframe1:GetPropertyChangedSignal("Value"):Connect(function()
		workspace.SpawnBus.CityBuildings.BuildingSet1:SetPrimaryPartCFrame(Cframe1.Value)
	end)
	local Tween1 = TweenService:Create(Cframe1,TweenInfo1, {Value = CFrame.new(-1053.61, 11.9, -839.46)})
	Tween1:Play()
	wait(1)
end

MoveSet1()

apparently, I think the problem is the function doesn’t know what “BuildingSet1”.

But thanks for the help.

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