Help with keybinds any else statements?

So I have this gun that has an option to put the gun down, and the other option for reload, I am trying to make an option to where you press M, and the gun changes idle animation, but I dont know where to fit this in to this portion of the script, any help?

	TempMouse.KeyDown:connect(function(Key)
		if string.lower(Key) == "r" then
			Reload()
		elseif string.lower(Key) == "g" then
			if not Reloading and not HoldDown and Module.HoldDownEnabled then
				HoldDown = true
				IdleAnim:Stop()
                if HoldDownAnim then HoldDownAnim:Play(nil,nil,Module.HoldDownAnimationSpeed) end
    		    if AimDown then 
                    TweeningService:Create(Camera, TweenInfo.new(Module.TweenLengthNAD, Module.EasingStyleNAD, Module.EasingDirectionNAD), {FieldOfView = 70}):Play()
			        CrosshairModule:setcrossscale(1)
			        --[[local GUI = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ZoomGui")
			        if GUI then GUI:Destroy() end]]
		        	Scoping = false
			        game:GetService("Players").LocalPlayer.CameraMode = Enum.CameraMode.Classic
			        UserInputService.MouseDeltaSensitivity = InitialSensitivity
			        AimDown = false
	        	end
			else
				HoldDown = false
				IdleAnim:Play(nil,nil,Module.IdleAnimationSpeed)
                if HoldDownAnim then HoldDownAnim:Stop() end
			end
		end
	end)

You can always use multiple elseif statements.

TempMouse.KeyDown:connect(function(Key)
	if key == "a" then
		-- handle A
	elseif key == "b" then
		-- handle B
	elseif key == "c" then
		-- handle C
		-- etc
	else
		-- anything you dont want to support
	end
end)

Note that there’s definitely better ways to accomplish things like this, rather than hardcoding if/elseif statements for every single input case, as that could be a very long, inefficient chain of events. An example would be having a specific function for each handled input, as such:

local input_events = {
	[Enum.KeyCode.A] = function(state, processed)
		 -- handle A
	end;
	[Enum.KeyCode.B]  = function(state, processed)
		-- handle B
	end;
}

UserInputService.InputBegan:Connect(function(input_object, game_processed)
	local keycode = input_object.KeyCode
	local event_function = input_events[keycode]
	if event_function then
		event_function(Enum.UserInputState.Began, game_processed) -- passing UserInputState Began so we can use the same function table for InputBegan and InputEnded, etc.
	end
end)
2 Likes

So I got this, and its not working, any help?
Local script:

	TempMouse.KeyDown:connect(function(Key)
		if string.lower(Key) == "r" then
			Reload()
		elseif string.lower(Key) == "g" then
			if not Reloading and not HoldDown and Module.HoldDownEnabled then
				HoldDown = true
				IdleAnim:Stop()
                if HoldDownAnim then HoldDownAnim:Play(nil,nil,Module.HoldDownAnimationSpeed) end
    		    if AimDown then 
                    TweeningService:Create(Camera, TweenInfo.new(Module.TweenLengthNAD, Module.EasingStyleNAD, Module.EasingDirectionNAD), {FieldOfView = 70}):Play()
			        CrosshairModule:setcrossscale(1)
			        --[[local GUI = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ZoomGui")
			        if GUI then GUI:Destroy() end]]
		        	Scoping = false
			        game:GetService("Players").LocalPlayer.CameraMode = Enum.CameraMode.Classic
			        UserInputService.MouseDeltaSensitivity = InitialSensitivity
			        AimDown = false
	        	end
			else
				HoldDown = false
				IdleAnim:Play(nil,nil,Module.IdleAnimationSpeed)
                if HoldDownAnim then HoldDownAnim:Stop() end
end
		elseif string.lower(Key) == "m" then
			if script.Parent.Switch.Value == false then
			script.Parent.GunScript_Server.AnimChan:FireServer()
			else
			if script.Parent.Switch.Value == true then
			script.Parent.GunScript_Server.AnimChan2:FireServer()	
			end
		end
		end
		end)

Server script:

script.AnimChan.OnServerEvent:connect(function(Player)

script.Parent.Switch.Value = true

script.Parent:WaitForChild("IdleAnim").AnimationId = "rbxassetid://3702971937"

end)

script.AnimChan2.OnServerEvent:connect(function(Player)

script.Parent.Switch.Value = false

script.Parent:WaitForChild("IdleAnim").AnimationId = "rbxassetid://3796753354"

end)

And no the changing “idle anim” does not work, the idle anim is a script in my module.

I went ahead and formatted your code for you, so that it is readable.

TempMouse.KeyDown:connect(function(Key)
	if string.lower(Key) == "r" then
		Reload()
	elseif string.lower(Key) == "g" then
		if not Reloading and not HoldDown and Module.HoldDownEnabled then
			HoldDown = true
			IdleAnim:Stop()
            if HoldDownAnim then
				HoldDownAnim:Play(nil,nil,Module.HoldDownAnimationSpeed)
			end
		    if AimDown then 
                TweeningService:Create(Camera, TweenInfo.new(Module.TweenLengthNAD, Module.EasingStyleNAD, Module.EasingDirectionNAD), {FieldOfView = 70}):Play()
		        CrosshairModule:setcrossscale(1)
		        --[[local GUI = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ZoomGui")
		        if GUI then GUI:Destroy() end]]
	        	Scoping = false
		        game:GetService("Players").LocalPlayer.CameraMode = Enum.CameraMode.Classic
		        UserInputService.MouseDeltaSensitivity = InitialSensitivity
		        AimDown = false
        	end
		else
			HoldDown = false
			IdleAnim:Play(nil,nil,Module.IdleAnimationSpeed)
            if HoldDownAnim then
				HoldDownAnim:Stop()
			end
		end
	elseif string.lower(Key) == "m" then
		if script.Parent.Switch.Value == false then
			script.Parent.GunScript_Server.AnimChan:FireServer()
		else
			if script.Parent.Switch.Value == true then
				script.Parent.GunScript_Server.AnimChan2:FireServer()	
			end
		end
	end
end)

Where is it failing? Have you put prints in at each step to debug where it’s failing?

Also, you need to create a new AnimationTrack (humanoid:LoadAnimation(Animation)) after changing the Animation’s AnimationId. Simply changing the AnimationId will do nothing.