How to make a player not move when a animation is playing?

I’m trying to make a animation system where if you press a button it plays a animation
However I’m not sure how to make certain animations play while the player can’t move ( example : sitting or kneeling animation )

local Player = game.Players.LocalPlayer
local IsSitting = false
local IsSitting1 = false
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.N then
		if not IsSitting then
			Player.Character:WaitForChild("Humanoid").WalkSpeed = 0
			Player.Character:WaitForChild("Humanoid").JumpHeight = 0
			IsSitting = true
			print("Player is moving")
		else
			IsSitting = false
			Player.Character:WaitForChild("Humanoid").WalkSpeed = 16
			Player.Character:WaitForChild("Humanoid").JumpHeight = 7.2
			print("Player Stopped Moving")
		end
	end
end)

UIS.InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.U then
		if not IsSitting1 then
			Player.Character:WaitForChild("Humanoid").WalkSpeed = 0
			Player.Character:WaitForChild("Humanoid").JumpHeight = 0
			IsSitting1 = true
			print("Player is moving")
		else
			IsSitting1 = false
			Player.Character:WaitForChild("Humanoid").WalkSpeed = 16
			Player.Character:WaitForChild("Humanoid").JumpHeight = 7.2
			print("Player Stopped Moving")
		end
	end
end)

This is what I tried but they glitch when you press them one after the other.
Both N and U play different sitting animations and I need them to make the player unable to move until you press the same button again.

2 Likes

You might wanna explain what happens when it glitches.

If I press U+N or N+U then I end up moving around while sitting down and not being able to move when the animation stops.

Try to change this line to

if inputObject.KeyCode == Enum.KeyCode.N and not isSitting1 then

Then replace this line;

With this:

if inputObject.KeyCode == Enum.KeyCode.U and not isSitting then

I assume that’ll fix it.

It didnt really change anything
Here is the full Script

local Player = game.Players.LocalPlayer
local Character = Player.Character
local UIS = game:GetService("UserInputService")
local CharAnimation
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if gameProcessedEvent then return end
	if inputObject.KeyCode == Enum.KeyCode.Z then -- Hands Behind Back
		animation("10351168482")
	elseif inputObject.KeyCode == Enum.KeyCode.G then -- Right Hand On Sword
		animation("10351343155")
	elseif inputObject.KeyCode == Enum.KeyCode.N then -- Sitting on Knees
		animation("10351423830")
	elseif inputObject.KeyCode == Enum.KeyCode.U then -- Sitting on one Knee
		animation("10353444538")
	end
end)
function animation(AnimationID)
	if Character then
		local UseLessAnimation = Character:FindFirstChild("AnimationCharacter")
		if CharAnimation then
			CharAnimation:Stop()
		end
		if UseLessAnimation then
			if UseLessAnimation.AnimationId == "rbxassetid://"..AnimationID then
				UseLessAnimation:Destroy()
				return
			end
			UseLessAnimation:Destroy()
		end
		local Animation =Instance.new("Animation",Character)
		Animation.Name= "AnimationCharacter"
		Animation.AnimationId = "rbxassetid://"..AnimationID
		CharAnimation= Character.Humanoid:LoadAnimation(Animation)
		CharAnimation:Play()
	end
end

-- Makes players unable to move during certain animations

local playermod = require(game:GetService("Players").LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playermod:GetControls()

local IsSitting = false
local IsSitting1 = false


UIS.InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.N and not IsSitting1 then
		if not IsSitting then
			Player.Character:WaitForChild("Humanoid").WalkSpeed = 0
			Player.Character:WaitForChild("Humanoid").JumpHeight = 0
			IsSitting = true
			print("Player is moving")
		else
			IsSitting = false
			Player.Character:WaitForChild("Humanoid").WalkSpeed = 16
			Player.Character:WaitForChild("Humanoid").JumpHeight = 7.2
			print("Player Stopped Moving")
		end
	end
end)

UIS.InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.U and not IsSitting then
		if not IsSitting1 then
			Player.Character:WaitForChild("Humanoid").WalkSpeed = 0
			Player.Character:WaitForChild("Humanoid").JumpHeight = 0
			IsSitting1 = true
			print("Player is moving")
		else
			IsSitting1 = false
			Player.Character:WaitForChild("Humanoid").WalkSpeed = 16
			Player.Character:WaitForChild("Humanoid").JumpHeight = 7.2
			print("Player Stopped Moving")
		end
	end
end)

And the game :
Team Change GUI - Roblox

Any ideas on how I can fix this?

Another thing you could try is moving the parts where you call the animation function into the function that makes the player unable to move. The issue might be the fact that the script tries to play two animations at a time whenever the player presses both buttons.

Its not that animations that get glitches it’s the fact that it glitches you walkspeed
You can try the game and press U+N or N+U and see how it glitches it
I have tried a lot of solutions and don’t really have any idea how to fix this
Team Change GUI - Roblox

I dont fully understand animations but there are properties like AnimationPriority or Weight