IsKeyDown not working

I’m trying to create a replacement Animate script for fun using welds but the crouching key is not working. Also the Animate script is server side.

--Variables
local Players = game:GetService("Players")
local Character = script.Parent
local Player = Players:GetPlayerFromCharacter(Character)
local RS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Client = ReplicatedStorage:WaitForChild("Client")

local New = CFrame.new
local Angles = CFrame.Angles

if not Player then error("Uh oh! This is a npc!.") end
if not Character:FindFirstChild("Torso") then error("Uh oh! This is a R15 rig.") end

--Setup
local Right_Leg = Instance.new("Weld", Character.HumanoidRootPart)

Right_Leg.Part0, Right_Leg.Part1 = Character.Torso, Character["Right Leg"]
Right_Leg.C0 = New(0.5, -2, 0)
Right_Leg.Name = "Right Hip Weld"

local Left_Leg = Instance.new("Weld", Character.HumanoidRootPart)

Left_Leg.Part0, Left_Leg.Part1 = Character.Torso, Character["Left Leg"]
Left_Leg.C0 = New(0.5, -2, 0)
Left_Leg.Name = "Left Hip Weld"

local Right_Arm = Instance.new("Weld", Character.Torso)

Right_Arm.Part0, Right_Arm.Part1 = Character.Torso, Character["Right Arm"]
Right_Arm.C0 = New(1.5, 0, 0)
Right_Arm.Name = "Right Shoulder Weld"

local Left_Arm = Instance.new("Weld", Character.Torso)

Left_Arm.Part0, Left_Arm.Part1 = Character.Torso, Character["Left Arm"]
Left_Arm.C0 = New(-1.5, 0, 0)
Left_Arm.Name = "Left Shoulder Weld"


local Torso = Instance.new("Weld", Character.HumanoidRootPart)

Torso.Part0, Torso.Part1 = Character.HumanoidRootPart, Character.Torso
Torso.C0 = New(0, 0, 0)
Torso.Name = "Torso Weld"

--Main

while true do
	RS.Stepped:Wait()
	
	--Foot planting
	local CFLeftLeg, CFRightLeg, IdleCFLeftLeg, IdleCFRightLeg = require(Client:WaitForChild("FootPlant"))(Character.HumanoidRootPart)
	if Character.Humanoid.MoveDirection.Magnitude > 0.001 then
		Right_Leg.C0 = Right_Leg.C0:Lerp(CFRightLeg, 0.3)
		Left_Leg.C0 = Left_Leg.C0:Lerp(CFLeftLeg, 0.3)
	else
		Right_Leg.C0 = Right_Leg.C0:Lerp(IdleCFRightLeg, 0.3)
		Left_Leg.C0 = Left_Leg.C0:Lerp(IdleCFLeftLeg, 0.3)
	end
	
	--Jumping
	local IsJumping,IsFreefalling = Character.Humanoid:GetState() == Enum.HumanoidStateType.Freefall, Character.Humanoid:GetState() == Enum.HumanoidStateType.Freefall
	
	if IsJumping or IsFreefalling then
		Right_Arm.C0 = Right_Arm.C0:Lerp(New(1.5, 2, 0), 0.3)
		Left_Arm.C0 = Left_Arm.C0:Lerp(New(-1.5, 2, 0), 0.3)
	else
		Right_Arm.C0 = Right_Arm.C0:Lerp(New(1.5, 0, 0), 0.3)
		Left_Arm.C0 = Left_Arm.C0:Lerp(New(-1.5, 0, 0), 0.3)
	end
	
	
	--Crouching
	local Key = UIS:IsKeyDown(Enum.KeyCode.C)
	print(Key)
	
	if Key then
		Torso.C0 = Torso.C0:Lerp(Angles(math.rad(50), 0, 0), 0.3)
	else
		Torso.C0 = Torso.C0:Lerp(Angles(0, 0, 0), 0.3)
	end
end

I believe the reason it is not working is because you are in a server script. You can only accept input from a local script so you would need to fire an event to let the server side know the key was pressed.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.