Tween Part Issue

Hey there! I am having a problem with tweening my pet, I am new to tweening parts so, bear with me.
My code:

local TweenService = game:GetService("TweenService")
local Object = script.Parent
function onPlayerAdded(player)
	local function onCharacterAdded(character)
		while true do
			wait(1)
		local TweenService = game:GetService("TweenService")
 
		 
		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
		 
		local tween = TweenService:Create(Object.Head, tweenInfo, character.Head.NeckRigAttachment)
		 
		tween:Play()
		end
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end
game.Players.PlayerAdded:Connect(onPlayerAdded)

I am getting this error

  09:28:47.900  Unable to cast to Dictionary  -  Server  -  Script:12
  09:28:47.900  Stack Begin  -  Studio
  09:28:47.900  Script 'Workspace.Regular Eggs.Common Egg.Dog.Script', Line 12 - function onCharacterAdded  -  Studio  -  Script:12
  09:28:47.900  Stack End  -  Studio

The "unable to cast to dictionary is coming from this line

local tween = TweenService:Create(Object.Head, tweenInfo, character.Head.NeckRigAttachment)

I know I am doing something wrong, I just don’t know what!

The 3rd argument of TweenService:Create are the Properties that will be tweened on the 1st argument.

TweenService:Create(Target, TweenInfo, {CFrame = Cframe.new(1,2,3)}

NechRigAttachment is an object, which the 3rd argument doesn’t accept.

So you are saying that I should switch argument 1 with argument 3 and vice versa?

Please understand what I am saying. The 3rd argument must be a Dictionary containing the properties that will be changed.

Yeah sorry I didn’t see your second edit until after…

Ok, my apologies ahead of time, I am just having trouble understanding…
I put in

local tween = TweenService:Create(Object.Head, tweenInfo, {CFrame = CFrame.new(character.Head.NeckRigAttachment.CFrame)})

and I am getting

  09:42:14.500  Workspace.Regular Eggs.Common Egg.Dog.Script:12: invalid argument #1 to 'new' (Vector3 expected, got CFrame)

Do you mind telling me exactly what the code is looking for in something like this in each argument

local tween = TweenService:Create(part, tweenInfo, goal)

I am sorry about all this…

Hey there!

There is no need to apologize.
The error you are getting right now is telling you that you have failed to create the CFrame.

Here is the problem.
You can use {CFrame = character.NeckRigAttachment.CFrame}.
While you are using CFrame.new() you can’t pass a CFrame as an argument.

Hope this was helpful!
Have a nice day.

Ok I tried that but this happens

local TweenService = game:GetService("TweenService")
local Object = script.Parent
function onPlayerAdded(player)
	local function onCharacterAdded(character)
		wait(5)
		script.Parent.Head.AlignOrientation.Attachment1 = character.Head.NeckRigAttachment
		while true do
			wait()
			local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
			 
			local tween = TweenService:Create(Object.Head, tweenInfo, {CFrame = character.Head.NeckRigAttachment.CFrame})
			 
			tween:Play()
		end
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end
game.Players.PlayerAdded:Connect(onPlayerAdded)

robloxapp-20210110-1652071_Trim.wmv (2.2 MB)
It goes to the middle of the world, how and why does that happen?
The parts around it also don’t stick to the original head, even though they are welded to it…

It’s because the CFrame of the Attachment is not actuall CFrame where it’s located in the Workspace. I belive this can be solved in doing this:

{CFrame = character.Head.NeckRigAttachment.CFrame:ToWorldSpace(character.Head.CFrame)}

What this does is converting the CFrame to World Space. You can read up more about this over here.

Hope this was helpful!
Have a nice rest of your day.

3 Likes

Also, attachments have attachment.WorldCFrame as a property that has that computed for them.

1 Like

Thanks, it worked! I have one question though, how would I make so that there is an off set from the players head but only on one side relative to the player, because if I tried to do a vector3 it would only offset on the world axis? Or should I just make an attachment stick out on the side of the player’s head?

Relative offsetting can be done like so.

finalOffsetCFrame = targetCFrame * targetOffsetCFrame
finalOffsetCFrame would be the resulting CFrame you asked about.
targetCFrame would be the starting CFrame you want offset from.
targetOffsetCFrame would be the offset you want to apply.

In your case, if you want an offset that is 5 studs right, 5 studs up, and 5 studs forward from the head you would do

character.Head.CFrame * CFrame.new(5,5,-5)
1 Like