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

My crouch script located in StarterCharacterScripts doesn't work at all?

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local char = player.CharacterAdded:Wait()

local hum = char.Humanoid

local animation = Instance.new("Animation", player.Character)
animation.AnimationId = "rbxassetid://05107306531"

animation.Priority = Enum.AnimationPriority.Movement

mouse.KeyDown:Connect(function(key)
  if key:lower() == "c" then
	hum:LoadAnimation(animation)
	animation:Play()
  end
end)

mouse.KeyUp:Connect(function(key)
  if key:lower() == "c" then
	hum:LoadAnimation(animation)
	animation:Play()
  end
end)

I put this local script I made in StarterCharacterScripts. Some stuff here I learnt with tutorials. If you spot anything please tell me!

5 comments

0

is it a LocalScript?

0

Also, did you intend for the player to hold C or press C to toggle crouching?

0

I do not recommend using mouse.Keydown. Use UserInputService instead.

0

Main reason why is because mouse.Keydown is not what roblox intends for you to do anymore, just like _G is replaced with module scripts. Use InputBegan and Enum.KeyCode

0

@Davy I wouldn't say _G has been replaced with ModuleScripts. _G is much quicker for simpler things like a single variable that you want to share between scripts. It's easier using _G for a variable as opposed to a ModuleScript. 

1 answer

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local Animate
local Humanoid = Player.Character:FindFirstChild("Humanoid")

UIS.InputBegan:Connect(function(input,gameProcessed)
		if input.KeyCode == Enum.KeyCode.LeftControl then
			print("Player Pressed LeftControl")
			local Animation = Instance.new("Animation",Player.Character)
			Animation.AnimationId = "rbxassetid://5002919222"
			Animate = Humanoid:LoadAnimation(Animation)
			Animate:Play()
	end
end)

UIS.InputEnded:Connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		print("Player DePressed LeftControl")
		Animate:Stop()
	end
end)

Here is a video of the script in action.

3 comments

0

This is why you should never use mouse.KeyDown.

0

Doesn't work

0

You need to use your animation not mine

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