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

CFrame messing up animation?

Asked by 5 years ago

So here is my script:

local lookVector = script.Parent.Parent.Adornee.Parent["LOOK AT ME"].Position
local cookingAnimation = script.Parent["Animation"]

script.Parent.TextButton.MouseButton1Click:connect(function() 
    local playerWhoClick = game.Players.LocalPlayer


    playerWhoClick.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-5.75, 3, 62.7), lookVector)

    playerWhoClick.Character.Humanoid.WalkSpeed = 0
    playerWhoClick.Character.Humanoid.JumpPower = 0
    local animationTrack = playerWhoClick.Character.Humanoid:LoadAnimation(cookingAnimation)

    animationTrack:Play()
    wait(1)
    animationTrack:Play()
    wait(1)
    animationTrack:Play()
    wait(1)

    print("Played.")

    playerWhoClick.Character.Humanoid.WalkSpeed = 16
    playerWhoClick.Character.Humanoid.JumpPower = 50

end)

Everything was working fine until I modified line 08. I added in the lookVector in it's initialization. What's strange is that is that it's not like after the CFrame line exits the script or anything, and the lines where I change the WalkSpeed and JumpPower work fine. I assume the problem is in the

animationTrack:Play()

lines. No animation plays. This wasn't happening before, please help.

1 answer

Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

Having good names for your objects that are in your game and variables in scripts comes with the benefit of organization and efficiency.

One of the problems I think you may have is that your script is running before things load in. When you identify the variable on Line 2 like script.Parent["Animation"], you should use the bracket method when working with tables. You don't have to but it's usually what you use it for.

Instead use :WaitForChild() or :FindFirstChild(). In our case, since we want the script to wait for our objects we will use :WaitForChild()

--Make sure these lead to the right location. If they don't the server will stay on an infinite wait.
local lookAtPart = script.Parent.Parent.Adornee.Parent:WaitForChild("LookAtMe")
local cookingAnimation =  script.Parent:WaitForChild("Animation")

script.Parent.MouseButton1Click:connect(function() 
    local playerWhoClick = game.Players.LocalPlayer

--I had change the name of lookVector to LookAtPart. We also access its Vector3 pos here.
    playerWhoClick.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-5.75, 3, 62.7), lookAtPart.Position)

    playerWhoClick.Character.Humanoid.WalkSpeed = 0
    playerWhoClick.Character.Humanoid.JumpPower = 0

One of the things you can fix and try using more are for loops. Most beginner scripters always repeat code that you can condense into a small piece of code.

We're going to use a counting for loop to go up to 3 after waiting a second and playing the animation.

local animationTrack = playerWhoClick.Character.Humanoid:LoadAnimation(cookingAnimation)

--For loops help a lot. Find reference for it on the wiki.
--I also don't know why you want the animation to play multiple times?
--Try using a function that plays it until its finish if this doesn't help.
    for i = 1, 3, 1 do
        animationTrack:Play()
        wait(1)
    end

    print("Played.")

    playerWhoClick.Character.Humanoid.WalkSpeed = 16
    playerWhoClick.Character.Humanoid.JumpPower = 50

end)

Make sure the defined variables lead to the right locations of your objects. Also since we're in a local script, the CFrame-ing of the player might not be seen by other players.

Ad

Answer this question