Ad
Log in to vote
0

How do you add debounce to an animation brick?

Asked by 2 months ago

So I'm making a game on roblox that is meant to play an animation when you touch a brick. It's about 8 seconds long, (idk if that helps) I've tried multiple things, but none work so far. Here is what I've made so far

function onTouched(hit) function debounce(deb) debounce(1) local human = hit.Parent:findFirstChild("Humanoid") if human then local anim = human:LoadAnimation(script.Parent.Animation) anim:Play() debounce(0) end end script.Parent.Touched:connect(onTouched)

Sorry for such a dumb question, but I need to out what problems I have with my script.

2 answers

Log in to vote
0
Answered by 2 months ago

You start by storing whatever it is you need in variables. In this case, you need the part that gives the animation, the animation itself. If it's a local script, you can load the animation to the local player from the script itself.

Next, you create a touch connection with the part and check if the part that hit it is actually a character. If it is a character, then you check if the animation is already playing. If it's not the case, then you play the animation.

Here's how:

-- Variables
local part = script.Parent
local animationToLoad = part:FindFirstChild("Animation")
-- Services
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local localChar = localPlayer.Character
local localHum = localChar:FindFirstChild("Humanoid")
-- Load the animation to the humanoid
local anim = localHum:LoadAnimation(animationToLoad)
local db = true

part.Touched:connect(function(hit)
    -- Check if the part that hit it is a character
    local char = hit.Parent
    local hum = char:FindFirstChild("Humanoid")

    if hum and db then
        db = false
        -- Check if the animation is already playing for the character. If it isn't, then play it.
        if not anim.IsPlaying then
            anim:Play()
        end
    end
end)

Let me know how it goes and if anything!

Ad
Log in to vote
0
Answered by 2 months ago

please attempt to correctly indent your code so others can read it but if you are using an animationtrack you can use the didloop event to make sure the animation fully completed once

refer to https://developer.roblox.com/en-us/api-reference/event/AnimationTrack/DidLoop for more details

local run = true
function onTouched(hit) 
    if run == true then 
        run = false
        local human = hit.Parent:findFirstChild("Humanoid") 
        if human then   
            local anim = human:LoadAnimation(script.Parent.Animation) 
            anim:Play() 
            anim.DidLoop:Connect(function() run = true anim:Stop end)
        end 
    end
end
script.Parent.Touched:connect(onTouched)

Answer this question