Making it so that an animation keeps playing if a player is holding down the mouse button1

I want to make it so that my dab animation not necessarily keeps playing over and over again but just stays in the dab position if the mouse button1 is held. I have used different topics like @SirMing and others, but I couldn’t get it to work. I know that I have to use user input service to do this, but nothing works. What is wrong here?

local player = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")

local function mousedown(inputtype)
	for i, mousebutton in pairs(userinputservice:GetMouseButtonsPressed()) do
		if mousebutton.UserInputType == inputtype then
			local loaddab = player.Character.Humanoid:LoadAnimation(script.Parent.DabAnimation)
			loaddab:Play()
			print("Played")
			return true
		end
	end
	return false
end

held = mousedown(Enum.UserInputType.MouseButton1)

Is the problem that the animation is not being played or is it that the dab animation is not holding?

The problem is that the animation is not loading in right now.

Try this script

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

Local Animation = Instance.new(“Animation”, char)
Animation.Name = “Animation”
Animation.Id = “animationid”
Local Play = char:WaitForChild(“Humanoid”):LoadAnimation(Animation)
Play:Play()

1 Like

Why not use an animation which is just the position looped?

You could play this at the end of the animation (you can connect to that with Stopped) or alternatively play it at a animation event (see the ‘Animation Events’ section in this tutorial). Of course, the AnimTrack can have Stop called upon it when the mouse button is lifted.

1 Like

The issue currently though is that the animation itself isn’t playing with the script I have.

Edit: It doesn’t print played either.


local player = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")
local loaddab = player.Character.Humanoid:LoadAnimation(script.Parent.DabAnimation)

local function mousedown(inputtype)
	for i, mousebutton in pairs(userinputservice:GetMouseButtonsPressed()) do
		if mousebutton.UserInputType == inputtype then
			if loaddab.IsPlaying == false then
				loaddab:Play()
			end
			print("Played")
			return true
		end
	end
	return false
	if loaddab.IsPlaying == true then
		loaddab:Stop()   -- Stop the animation when you are not holding mouse
	end
end

held = mousedown(Enum.UserInputType.MouseButton1)

This script gives the error: 13:09:21.364 - Players.HonestJar.Backpack.Dab.LocalScript:16: Expected ‘end’ (to close ‘function’ at line 5), got ‘if’; did you forget to close ‘then’ at line 8?.

I don’t think you can put stuff after the return.

My bad move the return after the last line of the function


local player = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")
local loaddab = player.Character.Humanoid:LoadAnimation(script.Parent.DabAnimation)

local function mousedown(inputtype)
	for i, mousebutton in pairs(userinputservice:GetMouseButtonsPressed()) do
		if mousebutton.UserInputType == inputtype then
			if loaddab.IsPlaying == false then
				loaddab:Play()
			end
			print("Played")
			return true
		end
	end
	if loaddab.IsPlaying == true then
		loaddab:Stop()   -- Stop the animation when you are not holding mouse
	end
	return false
end

held = mousedown(Enum.UserInputType.MouseButton1)

This gives the error: 13:38:35.663 - Players.HonestJar.Backpack.Dab.LocalScript:4: attempt to index nil with ‘Humanoid’

You need to load the animation as a variable before the loop,

Then play and stop and necessary.

1 Like

Right that is what the script does. But it still gets an error.

Try putting a couple :WaitForChild() in place that might solve this issue

Now I am getting this error…

Error: 13:52:09.786 - Players.HonestJar.Backpack.Dab.LocalScript:4: attempt to index nil with ‘WaitForChild’

Could you try this after getting the player.

Repeat wait() until player

local player = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")
while player.Character == nil do wait() end
local loaddab = player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent:WaitForChild("DabAnimation"))

local function mousedown(inputtype)
	for i, mousebutton in pairs(userinputservice:GetMouseButtonsPressed()) do
		if mousebutton.UserInputType == inputtype then
			if loaddab.IsPlaying == false then
				loaddab:Play()
			end
			print("Played")
			return true
		end
	end
	if loaddab.IsPlaying == true then
		loaddab:Stop()   -- Stop the animation when you are not holding mouse
	end
	return false
end

held = mousedown(Enum.UserInputType.MouseButton1)
1 Like

Sadly, this script doesn’t work…

maybe you can load the animation when the player clicks

At the end of the script, you only called the function once, it only checks for mouse once it’s loaded, change it to a while loop

local held = false
while true do
	held = mousedown(Enum.UserInputType.MouseButton1)
	wait()
end

This code when placed inside a LocalScript in Tool

-- LocalScript

local tool = script.Parent
local uis = game:GetService("UserInputService")
local mouseDown = false
local movePlay = true

tool.Equipped:Connect(function(mouse)
local humanoid = script.Parent.Parent:WaitForChild("Humanoid")
local move = humanoid:LoadAnimation(script:WaitForChild("Move"))	
	
	
mouse.Button1Up:Connect(function()
    mouseDown = false	
end)	
	
mouse.Button1Down:Connect(function()
uis.InputBegan:Connect(function(inp, gpe)
    if inp.UserInputType == Enum.UserInputType.MouseButton1 and not gpe then
        mouseDown = true			
		while mouseDown do
			wait(0.04)
			if movePlay then				
			   move:Play()		
			   movePlay	= false
			end			
		end
    end
end)
end)	
		
humanoid.AnimationPlayed:Connect(function(animationTrack)
	if animationTrack == move then
	animationTrack.Stopped:wait()
	movePlay = true	
	end		
end)	
	
end)

The animation continues if the player presses and holds the mouse button 1

If it is not working properly tell me so

3 Likes