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

How to :play() and :stop() using the same keys?

Asked by 4 years ago
Edited 4 years ago
local player = game.Players.LocalPlayer
local Animate
local Humanoid = player.Character:FindFirstChild('Humanoid')
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input,gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.C then
            if UIS:GetFocusedTextBox() == nil then
        Humanoid.WalkSpeed = 16
        local Animation = Instance.new("Animation", player.Character)
        Animation.AnimationId = "rbxassetid://03622617391"
        Animate = Humanoid:LoadAnimation(Animation)
        Animate:Play()
        wait()
            end
        if input.KeyCode == Enum.KeyCode.C then
            Animate:Stop()
            Humanoid.WalkSpeed = 36
        end
end
end

end)

This Doesn't Work

P.S. this is a crawling animation

Is there a way so if you click 'C' and it plays the animation(repeating) and if you click 'C' again then it stops?

I have been done this over an hour but I still don't get any ideas.

Ways I Have Tested

-Using BoolValues

--Using Variables

---The one used in the top script

----Use UIS.InputEnded

and about 10 more ways but they all don't work.

One of the issues are- even though I use a while, repeat loop if I stop moving the character the animation cancels.

Can anybody help?

0
also using an animation while moving automaticaly gets canceled by roblox, if u want an animation u can move around with then u have to script the movement Gameplayer365247v2 1055 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
local active = false
local player = game.Players.LocalPlayer
local Animate
local Humanoid = player.Character:FindFirstChild('Humanoid')
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input,gameProcessed)
        if input.KeyCode == Enum.KeyCode.C then
            if UIS:GetFocusedTextBox() == nil then
if not active then
       active  = true
        Humanoid.WalkSpeed = 16
        local Animation = Instance.new("Animation", player.Character)
        Animation.AnimationId = "rbxassetid://03622617391"
        Animate = Humanoid:LoadAnimation(Animation)
        Animate:Play()
wait(amount of time before you can click again, needs to be atleast 1 second cant be instant or it wont work)
        active = false
            end
end
        if input.KeyCode == Enum.KeyCode.C then
wait(1)
if active then
            Animate:Stop()
            Humanoid.WalkSpeed = 36
end
end
end

end)

i think this will work, created a check to see if u had the animation active

0
removed the keyboard check since its not needed Gameplayer365247v2 1055 — 4y
0
still, if i stop my character (stop my moving) the animation ends tree_tree00 48 — 4y
0
yes because using an animation will always be stopped automaticaly by movement, roblox made that, for an animation that plays even if u move u have to script that animation completely Gameplayer365247v2 1055 — 4y
Ad

Answer this question