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

Touch Event to play an Animation with an NPC?

Asked by 4 years ago

Do any of you know how to play an animation on an NPC when you touch a certain block? If any of you know how this is done, please let me know. I know this isn't a request site, but this is important if I want to finish my game. Please help me if you know the answer.

1 answer

Log in to vote
0
Answered by
Farsalis 369 Moderation Voter
4 years ago
Edited 4 years ago

Yeah, Sure, This should work:

local SP = script.Parent -- The Part To Be Stepped On
local Dummy = workspace.Dummy -- Change "Dummy" To Whatever You Want To Animate, Note* This Only Works Assuming That Your Only Animating One NPC
local Animation = Instance.new("Animation")
local Debounce = true -- Spam Detector
local LD = function(Parent, ID) Animation.Parent = Parent Animation.AnimationId = ID Dummy.Humanoid:LoadAnimation(Animation) end -- Loading Animation For Humanoid

SP.Touched:Connect(function(hit) -- "hit" Is The Part In Question. With This We Can Access The Player Who Stepped On The Block.
    local Hit = hit.Parent
    local AP = Dummy

        wait(.5)

    if Dummy:WaitForChild(Animation, 3) then -- Make Sure That There Is No Animation In Dummy
        print("Animation Found In: "..AP.Name.."..Deleting..")
        Animation:Destroy() -- Disonnecting Left Over Thread
    else
        print("No Animation Found In: "..AP.Name)   
    end

    if Hit:WaitForChild("Humanoid", 3) and Debounce then
        print("Found Humanoid From: "..Hit.Name.."!")
        Debounce = false
        LD(Dummy, "rbxassetid://AnimID") -- "AnimID" Change To Your Animation ID
        wait(.5) -- Can Change Time For Debounce Reset
        Debounce = true
    else
        print("Couldn't Find Humanoid From: "..Hit.Name.."....Or Debounce Is False")
    end
end)

https://gyazo.com/303b1a1f4be62aef223908530f1fe23c -- Where You Should Put The Script

A Bit Over - Complicated But, this should be smooth.

Hope This Helped!

~Farsalis

Ad

Answer this question