Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

1 comment

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

1 answer

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

3 comments

0

removed the keyboard check since its not needed

0

still, if i stop my character (stop my moving) the animation ends

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

This discussion is preserved as it was. New answers, comments, and votes are closed.