Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I made union part hat that floats above the player's head, but it don't work, why?

Asked by 3 years ago

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?

0
I think it's better for you to use a weld for this instead since it's not an actual hat. Then use tweening to move it up and down. radiant_Light203 1166 — 3y

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
3 years ago

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()
0
The code on line 6 keeps trying to find the CFrame as a child of the handle. It says;"Workspace.Target.Test.Handle.Script:8: attempt to index nil with 'CFrame'" Can you help me with this please? FastMasterMustache 5 — 3y
Ad

Answer this question