So when I run this code, it works but, it makes the crown like 20 studs away from the player and is glitchy when the player moves.
local HatAttachment = script.Parent:FindFirstChild("HatAttachment") local Handle = script.Parent local Head = script.Parent.Parent.Parent:FindFirstChild("Head") local Active = true Handle.CFrame = Head.CFrame * CFrame.new(0, 0.5, 0) while Active == true do HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 0.5, 0) wait(0.1) HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 0.6, 0) wait(0.1) HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 0.7, 0) wait(0.1) HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 0.8, 0) wait(0.1) HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 0.9, 0) wait(0.1) HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 1, 0) wait(0.1) HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 0.9, 0) wait(0.1) HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 0.8, 0) wait(0.1) HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 0.7, 0) wait(0.1) HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 0.6, 0) wait(0.1) HatAttachment.CFrame = Head.CFrame * CFrame.new(0, 0.5, 0) wait(0.1) end
Should I use a different way to set the crown position or a better way to animate it? Along with any reason why the crown gets set far away from the player?
Bare solution to the problem
You are multiplying CFrames
, while you should only add an offset.
HatAttachment.CFrame = Head.CFrame + Vector3.new(0, 0.5, 0)
Better method.
Use an infinite tween. It is way more efficient, as tweens are handled internally in the engine. To make sure crown moves with player, use Motor6D
and tween C0
property. Motor6D
is how character's limbs are animated, without falling off.
You can safely remove hat attachment
--local HatAttachment = script.Parent:FindFirstChild("HatAttachment") -- you do not need that anymore local Handle = script.Parent local Head = script.Parent.Parent.Parent:FindFirstChild("Head") local Active = true Handle.CFrame = Head.CFrame * CFrame.new(0, 0.5, 0) local weld = Instance.new("Motor6D") --this will be our new hat attachment weld.Part0 = Handle weld.Part1 = Head weld.Parent = Handle local TweenService = game:GetService("TweenService") local style = TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,-1,true) -- 1 second long tween -- sine easing (like a pendulum) -- direction in and out, so it eases at the beginning and end -- -1 means infinite repeats -- true means it will play in reverse once completed local target = {C0 = CFrame.new(0,1,0)} local tween = TweenService:Create(weld,style,target) tween:Play()